Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make debounce only apply to player who touched the brick?

Here's the script for the sound to play for the player who touches the brick to play... But I want the debounce to apply to the player who touches it only. Cause apparently when one player touches the brick the other has to wait until he can touch the brick too for the sound to play

local brick = script.Parent
local sound = script.Parent.Sound
debounce = false

brick.Touched:connect(function(playerWhoTouched)
player = game.Players:GetPlayerFromCharacter(playerWhoTouched.Parent)
if playerWhoTouched and not debounce then
debounce = true
play = sound:Clone()
play.Parent = player.PlayerGui
play:Play()
wait(120)
debounce = false
end
end)

2 answers

Rather than use an external Value object, you can just use a Table to store each Players' debounces individually:

local brick = script.Parent
local sound = script.Parent.Sound
debounce = {}

brick.Touched:connect(function(playerWhoTouched)
	player = game.Players:GetPlayerFromCharacter(playerWhoTouched.Parent)
	if player and not debounce[player] then
		debounce[player] = true
		play = sound:Clone()
		play.Parent = player.PlayerGui
		play:Play()
		wait(120)
		debounce[player] = false
	end
end)

One thing you can do is when a player joins make a Boolean value in their player (Players > Player > Debounce) and when the debounce should be there for them set that to true and then wait and set debounce (in their player) to false. So it checks if the player who touched it has a debounce of false and if it is true it won't work.

new script to add:

game.Players.PlayerAdded:connect(function(plr)
	local deb = Instance.new("BoolValue");
	deb.Name = "Debounce";
	deb.Parent = game.Players:FindFirstChild(plr.Name);
end;

Your script:

local brick = script.Parent
local sound = script.Parent.Sound
local debounce = game.Players[playerWhoTouched.Parent.Name):FindFirstChild("Debounce").Value;

brick.Touched:connect(function(playerWhoTouched)
	player = game.Players:GetPlayerFromCharacter(playerWhoTouched.Parent)
	if playerWhoTouched and not debounce then
		debounce = true;
		play = sound:Clone();
		play.Parent = player.PlayerGui;
		play:Play();
		wait(120);
		debounce = false;
	end;
end);

Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.