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
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
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