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

How can i make this dash script work the way i want?

I'm trying to make a script that makes the character model dash backwards when i double tap S. I managed to make the double tap part work but the problem is that the dash animation replays every single time i double tap S.

local UIS = game:GetService('UserInputService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local backDashEvent = replicatedStorage:WaitForChild('DashEvents').BackDashEvent


local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild('Humanoid')
local root = char:WaitForChild('HumanoidRootPart')


local animation = Instance.new('Animation')
animation.AnimationId = 'rbxassetid://04793471389'
local load = hum:LoadAnimation(animation)


local check = false


UIS.InputBegan:Connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.S and not check and not gameProcessed then
		check = true
		wait(0.2)
		check = false
	end
end)


UIS.InputBegan:Connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.S and check and not gameProcessed then
		
		load:Play()    <----- the animation replays every time i double tap S
		backDashEvent:FireServer(root)
		
		check = false
	end
end)

Line 32 i believe is where the problem is. I understand that i need some sort of debounce to put a cooldown on the animation but i'm not sure where i can insert it.

1 comment

0

You can use animation.Stopped, then animation:Stop() I think.

2 answers

✓ Accepted answer

you're right you do need a debounce

local UIS = game:GetService('UserInputService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local backDashEvent = replicatedStorage:WaitForChild('DashEvents').BackDashEvent


local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild('Humanoid')
local root = char:WaitForChild('HumanoidRootPart')


local animation = Instance.new('Animation')
animation.AnimationId = 'rbxassetid://04793471389'
local load = hum:LoadAnimation(animation)


local check = false
local db = true--debounce
local cooldown = 5 - cooldown for debounce


UIS.InputBegan:Connect(function(input,gameProcessed)
    if input.KeyCode == Enum.KeyCode.S and not check and not gameProcessed and db then
        check = true
        wait(0.2)
        check = false
    end
end)


UIS.InputBegan:Connect(function(input,gameProcessed)
    if input.KeyCode == Enum.KeyCode.S and check and not gameProcessed and db then

        load:Play()    <----- the animation replays every time i double tap S
        backDashEvent:FireServer(root)

        check = false
	db = false-- now you can't fire until its true
	wait(cooldown)
	db = true
    end
end)

I didn't test it out but this should be working

1 comment

0

Thanks, it worked!

I think this will work:

local animTrack = load:GetPlayingAnimationTracks ( ) --we need AnimationTracks because it has Stopped event

UIS.InputBegan:Connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.S and check and not gameProcessed then
		if animTrack.Stopped then --check the AnimationTracks is stopped or not
			animTrack:Play()
		end

		backDashEvent:FireServer(root)
		check = false
	end
end)

AnimationTrack

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