Debounce isnt working

i was making a diving script which just ragdolls the character uses pivot to and apply impulse since if youre contacting the ground any force applied is absolutely nullified, then added a debounce to add a cooldown between dives, which produced odd results,

for one, every other dive would have no cooldown to it for some reason, when printing out values its like the debounce variable had 2 instances of itself, since when setting it to true in the function somehow youd still be able to run it again even though it hadnt been set to false yet then that function would print out that the debounce variable was false despite having just been set to true,

heres the code

something i think should be known is that i added the raycasting variable
to see if that was a reason for the cooldown not working, i dont think so, that can mainly be disregarded, removing it also doesnt fix the code

local userInputService = game:GetService("UserInputService")

local ragdoll = game.ReplicatedStorage.ragdoll

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local raycasting = false

local cooldown

local spring1 = game.SoundService["spring1"]
local spring2 = game.SoundService["spring2"]

local springs = {spring1, spring2}

local rules = RaycastParams.new()
rules.FilterDescendantsInstances = {character}
rules.FilterType = Enum.RaycastFilterType.Exclude

local count = 0

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and not raycasting and not cooldown then
		count+=1
		print(count)

		raycasting = true
		-- this is just so that in the time it takes to raycast you cant dive again

		local result = workspace:Raycast(character.Torso.Position, Vector3.new(0,-4.5,0), rules)
		-- check if player is on ground

		raycasting = false

		if result then
			cooldown = true
			--for index, child in character:GetChildren() do
			--	print(child.Name)
			--end

			local torso = character:WaitForChild("Head")
			local direction = torso.CFrame.LookVector + Vector3.new(0,0.5,0)
			-- i do this before i ragdoll in hopes of it going in the right direction

			ragdoll:FireServer()

			local mass = character.PrimaryPart.AssemblyMass

			character:PivotTo(character:GetPivot() * CFrame.new(0,2,0) * CFrame.Angles(math.rad(-60),0,0))
			-- position off the ground since if youre in contact with the ground
			-- forces do nothing, friction is crazy?

			torso:ApplyImpulse(direction * mass * 40)
			springs[math.random(1,2)]:Play()

			task.wait(2)
			cooldown = false
		end
	end
end)

any help for why this debounce doesnt work would be appreciated, even just a reply, im losing my mind

maybe this will work. I put an “and cooldown == false”.

1 Like

didnt fix the problem but thanks for the reply regardless

after removing my raycast it seems like the debounce started working, any idea why this happens or how i could still use a raycast without having this issue

Instead of raycasting you can check if the humanoidstatetype is running/walking to determine if they are on ground, that’s one alternate approach

1 Like

thank you for the reply, ill try this out, ill mark you as the solution if it ends up working

yep looks like it works, thanks for the help i appreciate it pheesh

So one other approach to keep in mind for more complex debounce situations is to store the last time the action was successful using os.clock(), then use a check that the current os.clock() - lastActionTime is greater than or equal to your specified “cooldown”. This can help you avoid situations where a task.wait or task.delay style cooldown could be triggered multiple overlapping times

1 Like

could you give me an example of a situation where task.wait could overlap?

thanks for the further ensight

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.