How to Prevent RemoteEvents from Firing Across Multiple Workstation Scripts?

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:

  1. A RemoteEvent is fired to their client to prepare for a click (to activate the “Work” sequence).
  2. 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:

  1. The RemoteEvent.Work fires for both scripts since they are all listening for the same event.
  2. 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!

1 Like

I think it may be because of your input detection. Basically every time your firing the click ready remote it is creating another connection with the UserInputService and these do not automatically get cleaned up (this can also cause a memory leak). I would recommend either disconnecting them or an easier option would just be separate that section of the code and make it an independent segment.

Hope this helps :grin:

2 Likes

Thank you for the help! I solved the problem, also learned a good bit about disconnecting connections and cleaning up events, which gave me some useful ideas. I really appreciate it

1 Like