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

Cooldown/Debounce not working properly when using .Touched()?

Asked by
1uqt 2
2 years ago

So basically I made a brick that when is touched, its supposed to put fire particles all over your body, play a lava sound from your HumanoidRootPart, and would finally kill you:

function onTouched(hit)
    local debounce = false
    if hit.Parent:findFirstChild("Humanoid") then
        local Humanoid = hit.Parent:findFirstChild("Humanoid")
        local HRP = hit.Parent:findFirstChild("HumanoidRootPart")
        local Torso = hit.Parent:findFirstChild("Torso")
        local Head = hit.Parent:findFirstChild("Head")
        local LeftArm = hit.Parent:findFirstChild("Left Arm")
        local RightArm = hit.Parent:findFirstChild("Right Arm")
        local LeftLeg = hit.Parent:findFirstChild("Left Leg")
        local RightLeg = hit.Parent:findFirstChild("Right Leg")
        if not debounce then
            debounce = true
            wait(0.1)
            if Humanoid and Humanoid.Health > 0 then
                Sound = Instance.new("Sound", HRP)
                Sound.SoundId = "rbxassetid://142472270"
                MakeFire(Torso)
                MakeFire(Head)
                MakeFire(LeftArm)
                MakeFire(RightArm)
                MakeFire(LeftLeg)
                MakeFire(RightLeg)
                Sound.Playing = true
                Humanoid.Parent:BreakJoints()
                print("alive")
                debounce = false
            else
                print("dead")
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)

But my problem is that the code above is supposed to only run once if the character is alive. Even with the cooldown and checking if the player is alive, it still runs multiple times. This leads to more then one sound and particle being made, which just looks bad and sounds wrong.

Here's all of my code if anyone needs it:

function MakeFire(Path)
    Fire = Instance.new("ParticleEmitter", Path)
    Fire.Name = "Fire"
    Fire.Texture = "rbxassetid://275871881"
    Fire.LightEmission = 0.5
    Fire.Rate = 5
    Fire.Lifetime = NumberRange.new(0.5, 1.2)
    Fire.Rotation = NumberRange.new(-360, 360)
    Fire.RotSpeed = NumberRange.new(10, 100)
    Fire.Speed = NumberRange.new(5, 5)
    Fire.Acceleration = Vector3.new(0, 5, 0)
    Fire.Color = ColorSequence.new{
        ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 255, 0)),
        ColorSequenceKeypoint.new(0.323, Color3.fromRGB(255, 170, 0)),
        ColorSequenceKeypoint.new(0.686, Color3.fromRGB(255, 81, 0)),
        ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 0, 0))
    }
    Fire.Size = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 5.07),
        NumberSequenceKeypoint.new(1, 0.25)
    }

    Fire.Transparency = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 1),
        NumberSequenceKeypoint.new(0.284, 0),
        NumberSequenceKeypoint.new(0.863, 0.375),
        NumberSequenceKeypoint.new(0.9, 0),
        NumberSequenceKeypoint.new(1, 1)
    }
    Fire2 = Fire:Clone()
    Fire2.Parent = Path
    Fire.Name = "Fire2"
    Fire.Rate = 15
    Fire2.Size = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 4),
        NumberSequenceKeypoint.new(1, 0.25)
    }

    FireBlur = Instance.new("ParticleEmitter", Path) -- outer blur
    FireBlur.Name = "Blur"
    FireBlur.Texture = "rbxassetid://243664672"
    FireBlur.LightEmission = 0.3
    Fire.Rate = 25
    FireBlur.Lifetime = NumberRange.new(0.5, 1.2)
    FireBlur.RotSpeed = NumberRange.new(-200, 200)
    FireBlur.Speed = NumberRange.new(5, 5)
    FireBlur.Acceleration = Vector3.new(0, 5, 0)
    FireBlur.Color = ColorSequence.new{
        ColorSequenceKeypoint.new(0, Color3.fromRGB(205, 205, 0)),
        ColorSequenceKeypoint.new(0.25, Color3.fromRGB(205, 137, 0)),
        ColorSequenceKeypoint.new(0.5, Color3.fromRGB(205, 68, 0)),
        ColorSequenceKeypoint.new(0.75, Color3.fromRGB(205, 0, 0)),
        ColorSequenceKeypoint.new(1, Color3.fromRGB(120, 0, 0))
    }
    FireBlur.Size = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 4),
        NumberSequenceKeypoint.new(0.4, 2.26),
        NumberSequenceKeypoint.new(0.8, 0.75),
        NumberSequenceKeypoint.new(1, 0)
    }

    FireBlur.Transparency = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 0.325),
        NumberSequenceKeypoint.new(0.306, 0.363),
        NumberSequenceKeypoint.new(0.701, 0.5),
        NumberSequenceKeypoint.new(1, 1)
    }
    FireBlur2 = FireBlur:Clone() -- inner blur
    FireBlur2.Parent = Path
    FireBlur2.Name = "Blur2"
    FireBlur2.LightEmission = 0.5
    FireBlur2.Rate = 15
    FireBlur2.Color = ColorSequence.new{
        ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 255, 0)),
        ColorSequenceKeypoint.new(0.323, Color3.fromRGB(255, 170, 0)),
        ColorSequenceKeypoint.new(0.7, Color3.fromRGB(255, 81, 0)),
        ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 0, 0))
    }
    FireBlur2.Size = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 2),

        NumberSequenceKeypoint.new(1, 0.375)
    }

    FireBlur2.Transparency = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 1),
        NumberSequenceKeypoint.new(0.284, 0),
        NumberSequenceKeypoint.new(0.863, 0.375),
        NumberSequenceKeypoint.new(0.9, 0),
        NumberSequenceKeypoint.new(1, 1)
    }
    Sound.Playing = true
    Sound:Play()
    wait()
    Fire.Size = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 1.69),
        NumberSequenceKeypoint.new(1, 0.25)
    }
    Fire2.Size = NumberSequence.new{
        NumberSequenceKeypoint.new(0, 1.69),
        NumberSequenceKeypoint.new(1, 0.25)
    }
end
function onTouched(hit)
    local debounce = false
    if hit.Parent:findFirstChild("Humanoid") then
        local Humanoid = hit.Parent:findFirstChild("Humanoid")
        local HRP = hit.Parent:findFirstChild("HumanoidRootPart")
        local Torso = hit.Parent:findFirstChild("Torso")
        local Head = hit.Parent:findFirstChild("Head")
        local LeftArm = hit.Parent:findFirstChild("Left Arm")
        local RightArm = hit.Parent:findFirstChild("Right Arm")
        local LeftLeg = hit.Parent:findFirstChild("Left Leg")
        local RightLeg = hit.Parent:findFirstChild("Right Leg")
        if not debounce then
            debounce = true
            wait(0.1)
            if Humanoid and Humanoid.Health > 0 then
                Sound = Instance.new("Sound", HRP)
                Sound.SoundId = "rbxassetid://142472270"
                MakeFire(Torso)
                MakeFire(Head)
                MakeFire(LeftArm)
                MakeFire(RightArm)
                MakeFire(LeftLeg)
                MakeFire(RightLeg)
                Sound.Playing = true
                Humanoid.Parent:BreakJoints()
                print("alive")
                debounce = false
            else
                print("dead")
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)

while true do
    for i = 1,25 do
        wait(0.2)
        for _,v in pairs(script.Parent:GetChildren()) do
            if v:IsA("Texture") then
                v.Texture = string.format("rbxasset://textures/water/normal_d.dds",i)
            end
        end
        wait()
    end
end

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
2 years ago

In your first script your setting debounce to false every time you touch someone so it won’t work try placing it outside the ontouched function

0
Thank you, but now it won't do anything after you respawn 1uqt 2 — 2y
0
Nvm had to change something with my code 1uqt 2 — 2y
Ad

Answer this question