Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why Does this only sometimes fire? ContextActionService?

Asked by 9 years ago

This checks to see if a light button is in the vicinity and turns on the lights incorporated to that button. For some reason it only sometimes fires and sends the signal to the server even though all of the requirements are met. Help!

local script in backpack:

wait(1)
local interactables = game.Workspace.MapParts:WaitForChild("Interactables")
local player = game.Players.LocalPlayer
local contextAction = game:GetService("ContextActionService")

local SelectedLight = nil
local active = false

local range = 4.6

function InteractionS(name, inputState, inputObject)
    if SelectedLight ~= nil then
 game.ReplicatedStorage.LightEvent:InvokeServer(SelectedLight)
 script.LightS:Play()
end
end

function FindClosestLights()
 if not player.Character or not player.Character:FindFirstChild("Humanoid")then
  return
 end
 local Region = Region3.new(
  player.Character.Torso.Position-Vector3.new(range,range,range),
  player.Character.Torso.Position+Vector3.new(range,range,range)
)
 local CloseObjects = game.Workspace:FindPartsInRegion3(Region)
 local Lights = nil
 for _,x in next,CloseObjects do
  if x.Parent == interactables.Switches then
   Lights = x
  end
 end
 return Lights
end

function AntiCrash(name, inputState, inputObject)
 if inputState ~= Enum.UserInputState.Begin or not SelectedLight then return end
 spawn(InteractionS)
end


while wait() do
interactables = game.Workspace.MapParts:WaitForChild("Interactables")
 SelectedLight = FindClosestLights()
 if SelectedLight ~= nil and not active then
  active = true
  contextAction:BindActionToInputTypes("Interaction",AntiCrash,true,"e")
  contextAction:SetTitle("Interaction","Light ON/OFF")
 elseif active then
  active = false
  contextAction:UnbindAction("Interaction")
 end
end

Script in workspace works fine and without the AntiCrash the script crashes the game.

0
You shouldn't be doing this client-sided. A script this long in every player vs one script this long to iterate through all the players and check their distances.. I think it's a given that the latter is more efficient. Goulstem 8144 — 9y
0
AND you won't have to worry about supplements for FilteringEnabled, as any player's character is in the server! Goulstem 8144 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Your problem might be that FindPartsInRegion3 is limited to 100 parts (and it stops at 20 parts by default). Thus, if your range is high and then you walk inside a house that has 100 parts, any lights outside that house will not be found by FindPartsInRegion3 -- but this might not be the case for your situation since your 'range' is 4.6.

Instead, iterate over each light and check how far away it is from the player.

Another potential problem is that you only return one light (maybe that's what you want, I'm not sure). You don't even return the closest one, just a random one within range.

Further, in your while loop, you are continually binding and unbinding the action. Change line 49 to elseif SelectedLight == nil and active then. In addition, BindActionToInputTypes is deprecated. Look through ContextService and find a good replacement (ex BindAction looks similar).

Ad

Answer this question