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

Why is debounce is not working in this Touched:Connect script?

I am new to scripting, I tried to use debounce in this script, it does not seem to work, why? Also is my spacing/organization/neatness done correctly? Not fully sure how to order them and make the script neat.

script.Parent.Touched:Connect(function(hit)
    local players = game:GetService("Players")  --To get the service of players
    local playerHumanoid = hit.parent:WaitForChild("Humanoid")
    local hitfromPlayer = players:GetPlayerFromCharacter(hit.parent) --Getting the Player from its Character
	local debounce = false
	
	if not debounce then
		debounce = true
	

	    if hitfromPlayer then
	        playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed + 5
			
		print("work pls")
		wait (5)
		

		debounce = false
		
		end

    end
end)

1 answer

✓ Accepted answer

Everytime you touch the part it resets the debounce, hence not making it wait for the cooldown. Put the debounce outside of the function.

local debounce = false

script.Parent.Touched:Connect(function(hit)
    local players = game:GetService("Players")  --To get the service of players
    local playerHumanoid = hit.parent:WaitForChild("Humanoid")
    local hitfromPlayer = players:GetPlayerFromCharacter(hit.parent) --Getting the Player from its Character

    if not debounce then
        debounce = true


        if hitfromPlayer then
            playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed + 5

        print("work pls")
        wait (5)


        debounce = false

        end

    end
end)

1 comment

0

Oh I see, thank you

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