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

how do i make the debouce only affect the player that touched the parent of the script?

Asked by 4 years ago

I don't know how i can make a debounce only for the player that touches the part. If you find an answer can you please explain the script so i know how to do this in the future?

local sound = "rbxassetid://196059456"
debounce = false

if not debounce then
script.Parent.Touched:Connect(function(hit)
 if hit.Parent:FindFirstChild("Humanoid") then
  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          game.ReplicatedStorage.PlayMusic2:FireClient(player,sound)
        debounce = true
        wait(3)
        debounce = false
 end
end)

1 answer

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
4 years ago
Edited 4 years ago
local sound = "rbxassetid://196059456"

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not hit.Parent:FindFirstChild("Debounce") then --checks if not debounce then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            game.ReplicatedStorage.PlayMusic2:FireClient(player,sound)
            local tag = Instance.new("BoolValue") --adding debounce
            tag.Name = "Debounce" --naming it
            tag.Parent = hit.Parent --setting it to the hit's parent
            game.Debris:AddItem(tag,3) --clearing using service debris after 3 seconds
        end
    end
end)

Basically what this does is that it adds a BoolValue named Debounce and clears it out after 3 seconds. When the code runs again, it will check if theres anything named Debounce inside. Hope this helps. Edit: Also, the debounce checking comes AFTER the :Connect()-ing. For example:

local deb = false
script.Parent.Touched:Connect(function()
    if deb then return end --returns (ends current scope(?), returning something, in this case nothing) if deb is true. 
    deb = true
    delay(3,function()
        deb = false
    end)
end)
Ad

Answer this question