How to make checkpoint sound script apply to each player individually rather than the entire server?

I am in production of an obby based experience and within the experience there are checkpoints. What I tried to produce is that everytime when a player reaches the next checkpoint, it plays an audio.

The way my script is set is so that the audio plays when the checkpoint is touched and then it has a 60 second cooldown. (Although I’d prefer it to work in a way that everytime a checkpoint is touched by a player for the first time, it plays the audio rather than it playing every 60 seconds that it’s touched)

After some short testing, I have realised that if one player stepped on the checkpoint than the next player to touch the checkpoint will not hear the audio.

How do I make it so that the script only applies to players individually and that if one player touched it, the cooldown would only apply to that player rather than it applying to everyone?

Here is the script in question:

local Played = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and Played == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local Sound = game.Workspace.Sounds.Sound:Clone()
		Sound.Parent = Player.PlayerGui
		Sound:Play()

		Played = true
		wait(60)
		Played = false
	end
end)
1 Like

I would adjust the Played variable a bit to be a table, then store the true/false data in that table per player.
I’d probably also adjust the script to not include the wait, but instead know what time to listen for to come off cool down
Something like…

local Played = {}

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not Played[Player] or os.time() >= Played[Player] then
		    Played[Player] = os.time()+60 
	        	local Sound = game.Workspace.Sounds.Sound:Clone()
		        Sound.Parent = Player.PlayerGui
		        Sound:Play()

  
		end 
	end
end)
1 Like

Run it on the client in a localscript rather than in a serverscript, this way your changes don’t replicate to all other clients.