Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 8 years ago

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

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

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)

Ad
Log in to vote
1
Answered by 8 years ago

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!

Answer this question