This is a simple fix, but I can't get it

Hello fellow Devs! I could really use some help :slight_smile:

I am working on a script for a racing game.

Basically, I’ve set up a leaderboard to keep track & save players laps completed on the racetrack. I also added a wait(60) into the LapCounter part, so that players wouldn’t just back right up and “collect” laps. This part is working fine.

– I’m using a transparent part (LapCounter) at the finish line that will increase their lap value by one when they pass through it.

Here’s my problem:

I tested the game on a two player server and noticed that only one of the players was getting the lap.

So I understand the problem is that I’ve basically made this part “collectible” by the first player to pass through it, then the next player to pass through it after 60 seconds and so on…

But I obviously need it to work for every player that passes through the part lol
I tried changing the script inside of the part to be a LocalScript, but it didn’t work at all. The game ran as if the part didn’t even contain a script… I’ve spent hours searching the internet, but I don’t know how to fix it and I haven’t been able to find a solution. :frowning:

I’ll attach my code below. Thank you for reading and thanks in advance for your help. Stay creative :dizzy:

This is the script inside of the part I’m using to track player’s laps :

local part = script.Parent
local canGet = true

local function onTouch(otherPart)
	
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and canGet then
			canGet = false
			player.leaderstats.Lap.Value = player.leaderstats.Lap.Value + 1
			wait(60)
			canGet = true
		end
		
	end
	
	
end

part.Touched:Connect(onTouch)

Hello, I tried your script by making a part and making a Script inside it and pasting your code. It worked without any issues in Single Player & Multiplayer too.

Just make sure the Script is not a localscript its just a Script, as localscripts won’t work in Workspace. Also It might be because your wait time hasn’t finished.

1 Like

To elaborate on what @WaterJamesPlough said, to achieve the independent debounce for each player, you should assign a value to each player similar to your canGet variable.

I’ve modified the code you supplied above to debounce for each player instead of all players as soon as your part is touched, achieved through use of BoolValues.

(edit: make sure you set the wait back to 60, i had it at 5 for testing purposes)

local players = game:GetService("Players")
local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and player:FindFirstChild("canPass") then
			local cP = player["canPass"]
			if cP.Value then
				cP.Value = false
				player.leaderstats.Lap.Value = player.leaderstats.Lap.Value + 1
				wait(60)
				cP.Value = true
			end	
		end
	end
end

local function onPlayerAdded(player)
	local canPass = Instance.new("BoolValue", player)
	canPass.Name = "canPass"
	canPass.Value = true
end

part.Touched:Connect(onTouch)
players.PlayerAdded:Connect(onPlayerAdded)
3 Likes

maybe you could add multiple parts on the track which the player needs to pass through before claiming the lap, this would get rid of using wait(60)

Wow, this makes so much sense. I tested it in the game and it works!
I though about adding a debounce but I just don’t really understand the way they work, but this really helped me understand! Hopefully this will help me with my troubleshooting in the future.

Thank you so much for your help!

I may actually consider adding something like this. Thank you :slight_smile:

1 Like