Local Script is not detecting Parented Part

Hey Devs,
I am currently on an award system for a Race. The Script should give the Player +1 Point if he touched the Part. I made a local script for this because I want it that the remote event only fires 1 time and then repeats waiting until the Race got reset back. I added a delay for that but the problem is that when I put the script in a normal script in the award part, the debounce is not local (for example when 2 player hit the Part at the same time the first one who touched it gets the Points. It should work, but the Reward Part is in a Model, called Map1 in a Folder in ServerStorage. I have multiple maps, and 1 of them gets randomly parented to a Folder called RaceSystem in Workspace, every 60 seconds. Here is my Script: (also every Map has the same Reward Part)

local debounce = true

for i,v in pairs(game.Workspace.RaceSystem:GetChildren()) do
	if v:IsA("Model") then
		v.RewardPart.Touched:Connect(function()
			
				if debounce == true then
					game.ReplicatedStorage.RaceEvents.Points:FireServer()
					debounce = false
				repeat wait()

				until game.Workspace.RaceSystem.DestroyPart.CanCollide == true
					debounce = true
			end
		end)
	end
end


Sadly the script doesn’t works… Any help would be appreciated!
The Remote Event which gets fired (is in a Script in ServerScriptService):

game.ReplicatedStorage.RaceEvents.Points.OnServerEvent:Connect(function(Player)
	Player:WaitForChild("leaderstats").Points.Value += 1
end)


2 Likes

Where did you put the first script?

In StarterPlayerScripts. Should I move it in StarterGui?

I don’t think that will work, you should do something like make an invisible wall. When a player touches it, it will grant points.

I already explained that I only want that the player gets rewarded 1 Point only 1 Time. For this I have to add a debounce in a script in the “invisible wall”, but because the debounce is not local only 1 player will get the reward

It’s better to use only server scripts here. And instead of RemoteEvents use BindableEvents. It is safer and can fix some problems.

Then you can just use:

v.RewardPart.Touched:Connect(function(p)
	local plr = game.Players:GetPlayerFromCharacter(p.Parent)
	if plr then
		if debounce == true then
			game.ReplicatedStorage.RaceEvents.Points:Fire(plr) -- 'Points' event must be bindable
			debounce = false
			repeat wait()

			until game.Workspace.RaceSystem.DestroyPart.CanCollide == true
			debounce = true
		end
	end
end)
local debounce = true

script.Parent.Touched:Connect(function(p)
	local plr = game.Players:GetPlayerFromCharacter(p.Parent)
	if plr then
		if debounce == true then
			plr:WaitForChild("leaderstats").Points.Value += 1
			debounce = false
			repeat wait()

			until game.Workspace.RaceSystem.DestroyPart.CanCollide == true
			debounce = true
		end
	end
end)

If I use this in the Part and a Person touched the Part. Does the debounce go = false for the local Player, so when another player touched the part he also gets the point s+ debounce, or is it so that debounce goes false for everyone?

1 Like

Instead of a model try using separate parts

Only one player can touch this part.
If you want to more players can touch it then use this:

local bouncedPlayers = {}

script.Parent.Touched:Connect(function(p)
	local plr = game.Players:GetPlayerFromCharacter(p.Parent)
	if plr then
		if not table.find(bouncedPlayers, plr) then
			plr:WaitForChild("leaderstats").Points.Value += 1
			table.insert(bouncedPlayers, plr)
			repeat wait()

			until game.Workspace.RaceSystem.DestroyPart.CanCollide == true
			table.remove(bouncedPlayers, plr)
		end
	end
end)
1 Like

Thank you so much! I gave you the solution!

1 Like

I also want to insert this function in the Player Table:

game.ReplicatedStorage.RaceEvents.Fade:FireClient(plr)
			wait(1)
			game.ReplicatedStorage.RaceEvents.Died:FireClient(plr)
			HumanoidRootPart.CFrame = workspace.Checkpoints:FindFirstChild(tostring(plr:WaitForChild("TeleportedStage").Value)).CFrame + Vector3.new(0,3,0)

What do I have to add into the table for this?

I did not understand what you mean, can you explain more?

The script u gave me which I marked as a solution creates a player table for each plr who touched the part if its not there. It also removes the player table if a new race starts. U added ,plr) in the table. Now i gave the functions above. I just add them in the function when no plr table is there. Do i have to add something in the (…,plr) table function?

If you mean table.remove and table.insert, then no it is the functions of the table that delete or add an element to it.

local bouncedPlayers = {}

script.Parent.Touched:Connect(function(p)
	local plr = game.Players:GetPlayerFromCharacter(p.Parent)
	if plr then
		if not table.find(bouncedPlayers, plr) then
			plr:WaitForChild("leaderstats").Points.Value += 1
			game.ReplicatedStorage.RaceEvents.Fade:FireClient(plr)
			wait(1)
			game.ReplicatedStorage.RaceEvents.Died:FireClient(plr)
			HumanoidRootPart.CFrame = workspace.Checkpoints:FindFirstChild(tostring(plr:WaitForChild("TeleportedStage").Value)).CFrame + Vector3.new(0,3,0)
table.insert(bouncedPlayers, plr)
			repeat wait()

			until game.Workspace.RaceSystem.DestroyPart.CanCollide == true
			table.remove(bouncedPlayers, plr)
		end
	end
end)

Now where i added this function do i have to add in the table. Insert and the table.remove function something else?

You must add a player before wait(1), otherwise the player may touch the part several times until the second expires.

I dont understand. Can you explain a bit more?

local bouncedPlayers = {}

script.Parent.Touched:Connect(function(p)
	local plr = game.Players:GetPlayerFromCharacter(p.Parent)
	if plr then
		if not table.find(bouncedPlayers, plr) then
table.insert(bouncedPlayers, plr)
			plr:WaitForChild("leaderstats").Points.Value += 1
			game.ReplicatedStorage.RaceEvents.Fade:FireClient(plr)
			wait(1)
			game.ReplicatedStorage.RaceEvents.Died:FireClient(plr)
			HumanoidRootPart.CFrame = workspace.Checkpoints:FindFirstChild(tostring(plr:WaitForChild("TeleportedStage").Value)).CFrame + Vector3.new(0,3,0)
			repeat wait()

			until game.Workspace.RaceSystem.DestroyPart.CanCollide == true
			table.remove(bouncedPlayers, plr)
		end
	end
end)
1 Like