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

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

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.

5 comments

0

The script just stops after it set debounce to true.

0

Use print() to print a string for each condition passed. That will help you find where the script is stopping.

0

I already did that. I found out the script stops after it set debounce to true.

0

That means it's not finding the humanoid.

0

How can my script find the humanoid proberly then?

2 answers

✓ Accepted answer

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)

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.

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