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

Can somebody help me? Debounce makes my script break. Can't find the humanoid when I add debounce.

Asked by 3 years ago
Edited 3 years ago

Hello. I made this team script, and it will not work if I add debounce. I tested the script with prints and when it comes to the part where it finds the humanoid it won't work. Here is my script: I have some other locals, but that doesn't matter. The locals are assigned for teams.

local door = script.Parent
local debounce = false

door.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
            if plr.Team == Republic then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            elseif plr.Team == RI then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            elseif plr.Team == BlueTeam then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            elseif plr.Team == CG then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            elseif plr.Team == RG then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            elseif plr.Team == RC then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            end
        end
    end
end)

Hope you can help, thank you.

0
The script just stops after it set debounce to true. peter21340 41 — 3y
0
Use print() to print a string for each condition passed. That will help you find where the script is stopping. DeceptiveCaster 3761 — 3y
0
I already did that. I found out the script stops after it set debounce to true. peter21340 41 — 3y
0
That means it's not finding the humanoid. DeceptiveCaster 3761 — 3y
0
How can my script find the humanoid proberly then? peter21340 41 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

This happen because when a thing that doesn't has a Humanoid touched the door, debounce will set true then if-then will check is the thing touched has Humanoid but it dosen't has so the debounce = false line inside every if-then won't run. The next time something touch the door the rest of the code won't run because debounce is still true.

local debounce = false

door.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
            if plr.Team == Republic then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
            elseif plr.Team == RI then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
            elseif plr.Team == BlueTeam then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
            elseif plr.Team == CG then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
                debounce = false
            elseif plr.Team == RG then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
            elseif plr.Team == RC then
                door.CanCollide = false
                door.Transparency = .3
                wait(1)
                door.CanCollide = true
                door.Transparency = .5
                wait(1)
            end
        end
    debounce = false
    end
end)
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
3 years ago
Edited 3 years ago

Likely because the debounce variable is supposed to only be mutated whenever there is a humanoid within the touching object AND if any of the plr.Team conditions are satisfied. If the player is in none of those teams, it won't be set to false.

Answer this question