Local script affecting other players

Hello! I am having trouble with a system I created and I am wondering if anybody could help.

It is a timed section of the game where you are supposed to beat it in a certain amount of time, however if two (or more) people start the timer and one person fails, the timer will end for everyone. This also happens if someone hasn’t started the timer and they fail. You can obviously see how this causes an issue.

Here is a video showing the problem:

Here is the code used:

local buttons = game.Workspace.Map.RushZones.Buttons:GetChildren()
local doors = game.Workspace.Map.RushZones.Doors:GetChildren()
local stopTimer = game.Workspace.Map.RushZones.Failed:GetChildren()
local plr = game.Players.LocalPlayer

local timer = 60
local isRunning = false

-- main countdown function
local function countdown()
	plr.PlayerGui.RushzoneTimer.timer.Visible = true
	isRunning = true
	for i = timer, 1, -1 do
		if isRunning == true then
			plr.PlayerGui.RushzoneTimer.timer.Text = i
			task.wait(1)
		else
			break
		end
	end
	isRunning = false
	plr.PlayerGui.RushzoneTimer.timer.Visible = false
end

-- if you touch invisible block the timer ends
for i, v in pairs(stopTimer) do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if isRunning == true then 
				isRunning = false
			else
				return
			end
		end
	end)
end

-- button to start the timer
for i, button in pairs(buttons) do
	button.ProximityPrompt.Triggered:Connect(function(plr)
		button.ProximityPrompt.Enabled = false
			for i, door in pairs(doors) do
				door.Transparency = 1
				door.CanCollide = false
				countdown()
				door.Transparency = 0
				door.CanCollide = true
			end
		button.ProximityPrompt.Enabled = true
	end)
end

If anybody has an idea please help! Thank you! :slight_smile:

Where is the script located?
What kind of script (local or server) is it?

Both proximityPromot and touched will fire if other players/objects trigger it. For both you need to check that the player triggering the event == the local player.

E.g

button.ProximityPrompt.Triggered:Connect(function(plr)
  if plr == game.Players.LocalPlayer then

       -- continue here

You need to do the same for the .Touched event.

On mobile may be typos

1 Like

Script is located inside the GUI. (the GUI is obviously in startergui)

It is a local script.

I’m not sure if I’m doing this correctly but I’m getting an error:
image

Here is the updated code:

for i, v in pairs(stopTimer) do
	v.Touched:Connect(function(plr, hit)
		if hit.Parent:FindFirstChild("Humanoid") then -- this is line 34
			if plr == game.Players.LocalPlayer then
				if isRunning == true then 
					isRunning = false
				else
					return
				end
			end
		end
	end)
end


for i, button in pairs(buttons) do
	button.ProximityPrompt.Triggered:Connect(function(plr)
		if plr == game.Players.LocalPlayer then
			button.ProximityPrompt.Enabled = false
			for i, door in pairs(doors) do
				door.Transparency = 1
				door.CanCollide = false
				countdown()
				door.Transparency = 0
				door.CanCollide = true
			end
			button.ProximityPrompt.Enabled = true
		end
	end)
end
1 Like

As explained by BasePart | Roblox Creator Documentation you can only have 1 argument inside a Touched:Connect, in this case, this argument is plr. The other argument hit has a value of nil

2 Likes
v.Touched:Connect(function(plr, hit)

.Touched only returns one argument. Which is the basepart that hit it. If you want to get the player do

v.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacrter(hit.Parent)
    if Player then

-- continue
end)

May be typos cause on mobile x2

3 Likes

Thank you for this information, I didn’t know this

Ahh you’re a life saver! everything is fixed now. Thank you very much for your help :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.