Hello! I’m trying to learn scripting and have run into an issue with my game. Any feedback or suggestions for improvement are greatly appreciated, as I’m sure there are better ways to achieve what I’m trying to do.
I have two workstations, and each one has a “zone” that detects when a player enters. Once the player enters a zone:
- A RemoteEvent is fired to their client to prepare for a click (to activate the “Work” sequence).
- If the player exits the zone before clicking, another RemoteEvent is fired to cancel the “ClickReady” status.
I used ZonePlus to achieve this since I found the .Touched event a little too buggy
The issue arises when players interact with multiple workstations. Here’s my code structure:
Server Script inside both workstations:
local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = script.Parent
local zone = Zone.new(container)
zone.playerEntered:Connect(function(player) --Once the player enters the zone
remoteevents.ClickReady:FireClient(player) -- Fire click ready
zone.playerExited:Connect(function(player) -- If they exit...
remoteevents.Unready:FireClient(player) -- Fire "Unready" in their client
end)
remoteevents.Work.OnServerEvent:Connect(function(player)
-- Trigger "Work" sequence that teleports player, etc.
end)
end)
Client response:
clickCheck = true
activeCheck = true
remoteevents.ClickReady.OnClientEvent:Connect(function()
if activeCheck == true then -- Check if it's already active
activeCheck = false
clickCheck = true -- Set this to "True"
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and input.UserInputState == Enum.UserInputState.Begin and clickCheck == true then -- Check to see if client has left clicked, and if clickCheck is still true
4344
remoteevents.Work:FireServer() -- Fire the work sequence in the original script
end
end)
end
end)
remoteevents.Unready.OnClientEvent:Connect(function() -- If they exit...
if clickCheck == true then
clickCheck = false -- Set this to false so the if statement in ClickReady no longer passes
end
if activeCheck == false then
activeCheck = true
end
end)
The problem:
When I enter one workstation’s zone, everything works as expected. However, if I enter another workstation’s zone afterwards and trigger the sequence by clicking, both workstation scripts respond and fire their respective “Work” events.
This causes a conflict where the player is teleported between both workstations
I believe this is happening because:
- The RemoteEvent.Work fires for both scripts since they are all listening for the same event.
- The
playerEntered
connections remain active for previously activated workstations.
How can I ensure that the “Work” sequence only triggers for the correct workstation? Is there a way to isolate the behavior for each workstation without creating unique RemoteEvents for every single one?
Could this issue be related to how the zones are initialized, or am I missing something obvious?
Thanks in advance for your help!