So basically I have this script that plays a roll animation after the player presses a certain key, but I don’t know how to make it actually propel the player forward.
I’ve looked for solutions and found some that talk about using a BodyVelocity, but I’m not quite sure what they mean by that.
I’m sort of new to scripting, so please explain it like I’m 5. Thank you!
the script
|
|
v
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local RollDelay = true
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local RollAnim = Instance.new("Animation")
RollAnim.AnimationId = "rbxassetid://6494613462"
local RollTrack = animator:LoadAnimation(RollAnim)
mouse.KeyDown:Connect(function(key)
if key == "c" then
if not RollDelay then return end
RollTrack:Play()
RollDelay = false
wait(1)
RollDelay = true
end
end)
This allows the player to play the roll animation by pressing C, which is pretty great, but I still have no idea how to get it to move the player.
A way you can do that is by using a BodyVelocity that propels the user to the direction they’re facing for a short amount of time, it’s similar to how you’d propel the player in a dash system, this is how I believe you could do it
local Players = game:GetService("Players")
local debris = game:GetService("Debis")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local RollDelay = true
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local RollAnim = Instance.new("Animation")
RollAnim.AnimationId = "rbxassetid://6494613462"
local RollTrack = animator:LoadAnimation(RollAnim)
local function Roll()
local roll = Instance.new("BodyVelocity")
roll.MaxForce = Vector3.new(1,1,1) * 30000
roll.Parent = character.HumanoidRootPart
roll.Velocity = char.HumanoidRootPart.CFrame.LookVector * 60 --The main thing for propelling
debris:AddItem(roll,0.25) --After 0.25 seconds, remove the thing
end)
mouse.KeyDown:Connect(function(key)
if key == "c" then
if not RollDelay then return end
RollTrack:Play()
RollDelay = false
Roll()
wait(1)
RollDelay = true
end
end)
This makes it so whenever you press c and you can roll, it will play the aniamtion and also propel you hopefully
Thanks!
There were a few typos but I fixed those and it works almost perfectly.
One slight issue I found is the animation completely breaks (doesn’t play how it’s supposed to) when I press C and I’m not moving.
Is there any way to make it only function when the player is moving or possibly a way to fix the animation breaking?
I don’t really know how to put it in words, so I may have to record a video, but basically the animation just kind of glitches out when I press C and I’m not moving
like only the last half plays idk
You can try putting a check to see if the player is moving or not. Try adding the line I added into your event when c is pressed
mouse.KeyDown:Connect(function(key)
if key == "c" then
if not RollDelay then return end
if humanoid.MoveDirection == Vector3.New(0,0,0) then return end
RollTrack:Play()
RollDelay = false
Roll()
wait(1)
RollDelay = true
end
end)
Humanoids have a property called MoveDirection, it tells you where the humanoid is currently moving in terms of direction, if it’s 0 on XYZ, then they are standing still
It could be that well since those animations would probably me incompatible. If it doesn’t work even if you go in r6, try my suggestion. And even if it works when in r6, i think it would be good to include what I said since it wouldn’t amke sense to suddenly be rolling without moving at first