I’m trying to make a movement system where you can slide, and I want the player to be able to jump to cancel the slide.
I got the script to work but randomly it seems to just not work properly, jumping but not cancelling the slide or sometimes breaking the animations
I’ve tried to put a wait before the actual jump cancel can happen, but that didn’t really help me much tbh.
-- Variables and get service
local UIS = game:GetService("UserInputService")
local keybind = Enum.KeyCode.LeftShift
local Cancel = Enum.KeyCode.Space
local canslide = true
local player = game:GetService('Players').LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local HumanoidRootPart = char:WaitForChild('HumanoidRootPart')
local Humanoid = char:FindFirstChild("Humanoid")
local loop = true
local HasDoubleJumped = false
local PreviousJump = tick()
local Dashed = false
-- Other settings
-- Animation stuff
local SlideAnim = Instance.new("Animation")
SlideAnim.AnimationId = "rbxassetid://14431552792"
local playAnim = Humanoid:LoadAnimation(SlideAnim)
-- Sound stuff
local RunSound = HumanoidRootPart:WaitForChild("Running", 10)
local JumpSound = HumanoidRootPart:WaitForChild("Jumping", 10)
local slideSound = Instance.new("Sound")
slideSound.SoundId = "rbxassetid://14433837493"
slideSound.Parent = char.HumanoidRootPart
slideSound.Volume = 0.1
-- Double jump
local function DoubleJump()
if tick() - PreviousJump >= 0.2 then
if Humanoid:GetState() == Enum.HumanoidStateType.Freefall and not HasDoubleJumped then
HasDoubleJumped = true
Humanoid.JumpPower = 70
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
end
Humanoid.StateChanged:Connect(function(old, new)
if new == Enum.HumanoidStateType.Landed then
HasDoubleJumped = false
Humanoid.JumpPower = 50
elseif new == Enum.HumanoidStateType.Jumping then
PreviousJump = tick()
end
end)
-- This function limits Dash to only be usable once before landing
Humanoid.StateChanged:Connect(function(Landed)
if Landed == Enum.HumanoidStateType.Landed then
Dashed = false
else
end
end)
UIS.JumpRequest:Connect(DoubleJump)
-- Dash and Slide part
UIS.InputBegan:Connect(function(input,gameProcessed)
if gameProcessed then return end
if not canslide then return end
if input.KeyCode == keybind then
if freefall == true and Dashed == false then -- dashes if in the air
canslide = false
Dashed = true
print("You have dashed")
local dash = Instance.new("BodyVelocity")
dash.MaxForce = Vector3.new(math.huge,0,math.huge)
dash.Velocity = char.HumanoidRootPart.CFrame.lookVector * 80
dash.Parent = char.HumanoidRootPart
for count = 1, 7 do
if Humanoid.Jump == true then
dash:Destroy()
UIS.JumpRequest:Connect(DoubleJump)
end
wait(0.1)
dash.velocity *= 0.7
end
canslide = true
dash:Destroy() -- destroys instance once finished
elseif freefall == false then -- slides if on ground
canslide = false
print("You have slided")
playAnim:Play()
RunSound.Volume = 0 -- disables running sound
slideSound:Play()
char.HumanoidRootPart.CanCollide = false
local slide = Instance.new("BodyVelocity") -- creates a body velocity
slide.MaxForce = Vector3.new(math.huge,0,math.huge)
slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 60
slide.Parent = char.HumanoidRootPart
wait(0.1)
for count = 1, 7 do
if Humanoid.Jump == true then
local position1 = HumanoidRootPart.CFrame.lookVector
local position2 = HumanoidRootPart.CFrame.lookVector * 1
local duration = 0.1
local direction = position2 + position1
local force = direction / duration + Vector3.new(0, workspace.Gravity * duration * 0.5, 0)
char.HumanoidRootPart.CanCollide = true
HumanoidRootPart:ApplyImpulse(force * HumanoidRootPart.AssemblyMass)
RunSound.Volume = 1
playAnim:Stop()
slideSound:Stop()
slide:Destroy()
wait(0.2)
canslide = true
end
wait(0.1)
slide.velocity *= 0.7
end
char.HumanoidRootPart.CanCollide = true
RunSound.Volume = 1
playAnim:Stop()
slide:Destroy()
wait(0.2)
canslide = true
end
end
end)
while loop == true do wait(0.01) -- detects if the player is in the air or on the ground.
if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
freefall = true
else
freefall = false
end
end