local g_switch = {} local hasTrap = {} trap_name = {} pressE = 0 function spearswitch_init_name(e,name) g_switch[e] = "on" trap_name[e] = name hasTrap[e] = nil end function spearswitch_main(e) if hasTrap[e] == nil then for a,_ in pairs(trap_name) do if a ~= e then if trap_name[a] == trap_name[e] then hasTrap[e] = a break end end end --if no trap found with same name we will just grab the closest trap if hasTrap[e] == nil then local tdist = 99999 local tx1,ty1,tz1 = GetEntityPosAng(e) for a,_ in pairs (trap_name) do if a ~= e then local tx2,ty2,tz2 = GetEntityPosAng(a) local tdx, tdy, tdz = tx1-tx2,ty1-ty2,tz1-tz2 local tdist2 = tdx*tdx+tdy*tdy+tdz*tdz if tdist2 < tdist then hasTrap[e] = a end end end end --if still no trap then we have an error if a == trap_name[e] then PromptLocal(e,"no trap found!") return end else local PlayerDist = GetPlayerDistance(e) if PlayerDist < 100 and g_PlayerHealth > 0 then if g_switch[e] == "on" then PromptLocal(e,"Press E to deactivate this trap" ) if g_KeyPressE == 1 then if pressE == 0 then g_switch[e] = "offing" SetAnimation(0) PlayAnimation(e) g_Entity[e]['animating'] = 1 SetActivated(e,4) SetActivated(hasTrap[e],3) end pressE = 1 end elseif g_switch[e] == "off" then PromptLocal(e,"Press E to activate this trap" ) if g_KeyPressE == 1 then if pressE == 0 then g_switch[e] = "oning" SetAnimation(0) PlayAnimation(e) g_Entity[e]['animating'] = 1 SetActivated(e,3) SetActivated(hasTrap[e],1) end end end if g_Entity[e]['activated'] == 3 then if g_Entity[e]['animating'] == 0 then SetActivated(e,1) g_switch[e] = "on" end elseif g_Entity[e]['activated'] == 4 then if g_Entity[e]['animating'] == 0 then SetActivated(e,2) g_switch[e] = "off" end end end if g_KeyPressE == 0 then pressE = 0 end end end