Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make my A,S,D dash script work? And how can I add animations & cooldowns?

Asked by 6 years ago
Edited 6 years ago

I've been working on a game and just got started with scripting. My forward dash works fine, you double tap W and there you have it, a dash, but there's no animation and cooldown and I don't know how to add that. Also, I tried using the same script for A,S,D dashes but changing the keys and velocity but it won't do the actual dash, instead the character turns to said direction, let's say D, a right dash, and dashes forward. So basically, it's still a forward dash but with different keys, here's the script:

local plr = game.Players.LocalPlayer
local Char= plr.Character or plr.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")

local Tapped = false
local Time = 0.25

UserInputService.InputBegan:Connect(function(Input, GameStuff)
 if GameStuff then return end
 if Input.KeyCode == Enum.KeyCode.W then
  if not Tapped then
   Tapped = true
   wait(Time)
   Tapped = false
  else
   Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*400
  end
 end
end)  `

2 answers

Log in to vote
0
Answered by
Asentis 17
6 years ago
Edited 6 years ago

(it's late where I live so things might not work, tell me if it doesn't and I'll try fixing it) Having a wait between taps doesn't do much unless you have a debounce which will cancel all taps until the debounce is true which can be by a wait.

Research a bit on debounce but anywho:


-- Updated for animations local plr = game.Players.LocalPlayer local Char = plr.Character or plr.CharacterAdded:Wait() local Humanoid = Char:WaitForChild("Humanoid") local UserInputService = game:GetService("UserInputService") local YourAnimationId = "" local LoadAnimation = Humanoid:LoadAnimation(YourAnimationId) local Tapped = false local Time = 0.25 local Debounce = true UserInputService.InputBegan:Connect(function(Input, GameStuff) if GameStuff then return end if not Debounce then return end Debounce = false if Input.KeyCode == Enum.KeyCode.W then Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*400 LoadAnimation:Play() wait(Time) LoadAnimation:Stop() -- You can remove this if you don't want it to be cut off Debounce = true else return end end)

Haven't actually tested it but it should work, if it doesn't just tell me.

0
Thanks, but yeah, I tried it and it doesn't work. I don't understand why keith349 0 — 6y
0
It doesn't work because you cant load an animation id , you need to make an 'animation' object and then paste the animation into it. User#24960 0 — 6y
0
@TheFuzi that's bs, all animations use animation IDs DeceptiveCaster 3761 — 6y
0
No, he/she is right. You have to create an animation using Instance.new, give it an animation ID and load it in that way. It was 5 AM so kind of forgot lol Asentis 17 — 6y
Ad
Log in to vote
0
Answered by 3 years ago

I am fully aware this was 2 years ago, but I stumbled upon it, it didn't work, and I know how to fix it.

Now, me 2 years ago would be very proud of me now for knowing both what's happening in the script and how to fix it, and to honor 2 year ago me's legacy I will be helping others that were as clueless as I was.

I strongly advise if you want to learn to do this kind of scripting by yourself, don't just copy and paste, read through my explanations and ask questions if you need to.

One more thing, before we start, I will be assuming you have a basic knowledge of Lua and Roblox Lua (Since they can be different at times).

Okay, first we'll start by assigning the necessary variables: local plr = game.Players.LocalPlayer local Char = plr.Character or plr.CharacterAdded:Wait() local Humanoid = Char:WaitForChild("Humanoid") local UserInputService = game:GetService("UserInputService") All we're doing here is preparing some variables for what comes later in the script, you should understand these, but if you don't, you can find them documented here: https://developer.roblox.com/en-us/api-reference/.

Next, we'll prepare the animation. In the original script for this, they tried to load the animation id into the humanoid directly, that does not work. Instead you have to make an animation instance, put the animation id into the instance, then load the instance into the humanoid, which is simpler than it sounds: local animation = Instance.new("Animation") -- Create the animation instance animation.AnimationId = "rbxassetid://Your Animation Id" -- Put the animation id into the instance local loadedAnimation = Humanoid:LoadAnimation(animation) -- Load the instance into the humanoid What this is doing is making it so we can play the loadedAnimation whenever we want. Again, if you don't understand what's happening in this, the link I put for the variables we assigned will also explain this.

Alright, the next thing we have to do is assign something called debounce, which is basically a cooldown. To be clear, this is fully optional, but animations and dashing may behave weirdly if we don't have this: local Debounce = true -- Assigning debounce local BackupCooldownTime = 2 -- A backup cooldown in case waiting for out animation to stop playing doesn't work. Debounce, even though it's pretty much community made, is also listed on the Dev API link I put above, in case you're confused by it.

The last variable we're going to define will be for customizability: local inputKey = "E" -- Change to whatever key you want to trigger the dash with. All we're doing here is assigning what key will make our character dash.

And that's all our variables defined, now we can get to the true brain of the script. We're now going to be putting to use the variables we defined before, and we're going to start off simply by detecting player input: UserInputService.InputBegan:Connect(function(Input) -- Detects player inputs print("Input detected") -- Lets us know our detection is working when we press keys while playing end) This is actually all we need to do to detect all inputs, but we need to do a bit more to be able to tell if the input was our assigned input key, so lets change what we just wrote to this: ``` UserInputService.InputBegan:Connect(function(Input)

print("input detected")

if Input.KeyCode == inputKey then -- Detects when we press our assigned key.
    print("Input key detected")
end

end) This will make it so that if the player presses any key other than the input key, it won't make them dash. Next up is making the debounce work, if you don't want a cooldown, skip this part. For those that do want a cooldown, we're putting this code in the if statement we just made: if not Debounce then return end -- If the debounce is set to false, exit the if statement. Debounce = false -- If it wasn't set to false then set it to false. ``` So, this is pretty simple, not much going on, you don't really have to understand it, but it's good if you do.

Next, we're going to actually be making the character dash. In the original script the person put 400 as their multiplier, personally I found this to be too much so I set it to 200, but it's up to you what yours is going to be. The multiplier controls how far the player will go, by the way. Anyways, to add the dashing, put this in your if statement AFTER the debounce: Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*200 How this works is, the humanoid root part or HRP for short has a velocity in it. This velocity can be changed, though. So what we're doing is changing the velocity relative to the direction the character is facing. If you wanted to make it so that the dash speed is relative to the player speed (Slower players = less distance on the dash, faster players = more distance on the dash) you would change that line to this: Char.HumanoidRootPart.Velocity = (Char.HumanoidRootPart.CFrame.lookVector*200)*(Char.Humanoid.Walkspeed/16) I know how confusing and annoying CFrames can be for a new dev, heck I don't even fully get them, so I very highly advise you to check them out on the Dev API that I've mentioned a million times already.

We're almost done, but we still need to play the animation and reset the debounce after it's done playing. This is fairly simple, too, the following code also goes in your if statement, again AFTER the debounce. loadedAnimation:Play() -- Plays our animation. loadedAnimation.Stopped:Wait() -- Waits for the animation to end, make sure your animation isn't looped, or else the script will never continue. Debounce = true -- Resets the debounce after our animation has ended. And with that, we have our if statement, but we're STILL not done. I've experienced a weird, but rare occurrence where our debounce isn't set to true after waiting for the loaded animation to stop, I'm sure this is some sort of studio bug, but just to be safe we'll add a backup.

Keep in mind, we're adding this backup after and outside of the if statement. if Debounce ~= true then wait(BackupCooldownTime) Debounce = true while Debounce ~= true do Debounce = true end end All this really does is ask if our debounce reset was successful, and if it wasn't we'll just wait our cooldown then reset the debounce again. Then, if it's still unsuccessful, it keeps doing this until it is.

And that's our entire script! If you want to see the whole thing together, or were confused where some parts of it went, here you go: ``` local plr = game.Players.LocalPlayer local Char = plr.Character or plr.CharacterAdded:Wait() local Humanoid = Char:WaitForChild("Humanoid") local UserInputService = game:GetService("UserInputService")

local animation = Instance.new("Animation") animation.AnimationId = "rbxassetid://7218115009" local loadedAnimation = Humanoid:LoadAnimation(animation)

local Debounce = true local BackupCooldownTime = 2

local inputKey = "E"

UserInputService.InputBegan:Connect(function(Input)

print("input detected")

if Input.KeyCode == inputKey then
    print("dash key pressed")
    if not Debounce then return end
    Debounce = false
    Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*200
    loadedAnimation:Play()
    loadedAnimation.Stopped:Wait()
    Debounce = true
end
if Debounce ~= true then
    wait(BackupCooldownTime)
    Debounce = true
end

end)

```

Hope you all enjoyed! Message me or reply if you have any questions.

0
Hey so the problem is the dash key doesnt work tunganhbk008 0 — 3y

Answer this question