I’m working on a slide mechanic right now. The issue I’m encountering is when the slide gets too slow, I want to make it stop but I don’t know how. (for example if the player’s magnitude is less than 5 then the slide will stop.
Here’s my code (sorry it’s kinda bad im not a programmer):
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local char = script.Parent
local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://0" -- I'll add it here later
local keybind = Enum.KeyCode.C
local canslide = true
local humanoid = char:WaitForChild("Humanoid")
UIS.InputBegan:Connect(function(input, gameprocessed)
if gameprocessed then return end
if not canslide then return end
if humanoid.MoveDirection.Magnitude > 0 and humanoid:GetState() == Enum.HumanoidStateType.Running then
if input.KeyCode == keybind then
canslide = false
local playAnim = char.Humanoid:LoadAnimation(slideAnim)
playAnim:Play()
local slide = Instance.new("BodyVelocity")
slide.MaxForce = Vector3.new(1,0,1) * 15000
slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 50
slide.Parent = char.HumanoidRootPart
local tweenInfo = TweenInfo.new(8, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
local slideTween = TweenService:Create(slide, tweenInfo, {Velocity = Vector3.new(0,0,0)})
slideTween:Play()
--playAnim.Stopped:Wait()
--wait(0.8)
local isShiftKeyDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftShift)
if isShiftKeyDown then
humanoid.Jumping:Wait()
else
slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 25
humanoid.Jumping:Wait()
end
slideTween:Cancel()
slide:Destroy()
canslide = true
end
end
end)
I’m wanting to fix this part:
if isShiftKeyDown then
humanoid.Jumping:Wait()
else
slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 25
humanoid.Jumping:Wait()
end
Here’s an example of what I want it to do:
if isShiftKeyDown then
humanoid.Jumping:Wait() or humanoid.RootPart.Velocity.Magnitude < 5
else
slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 25
humanoid.Jumping:Wait() or humanoid.RootPart.Velocity.Magnitude < 5
end
Obviously this code doesn’t work – but hopefully it clarifies what I’m trying to say.
If you have any questions, feel free to ask. Thanks!!
If I understand your example code correctly you want the character to slide until the character is moving slowly or until you cancel the slide by jumping, is that correct?
If so, when you call humanoid.Jumping:Wait() your code will pause until the character jumps. So if the character never jumps, the code will stay paused there forever. This creates a problem. Something you could try instead is putting something like this after you start playing the tween:
(code not tested)
local jumpingEvent = nil
local cancelledByJump = false
-- temporarily listen to the jumping event
jumpingEvent = humanoid.Jumping:Connect(
function(jumpActive) -- jumpActive is true if you start jumping, false if you stopped jumping
if jumpActive then -- we are interested in when you *start* jumping
cancelledByJump = true
jumpingEvent:Disconnect() -- disconnect this event so that the next jump action does not trigger this code anymore
jumpingEvent = nil
end
end
)
-- keep looping until the character is moving slowly or until the cancelledByJump flag is set
while not cancelledByJump do
if humanoid.RootPart.Velocity.Magnitude < 5 then
break -- break out of the while loop
end
game:GetService("RunService").Heartbeat:Wait() -- wait until the next 'frame'
end
-- if you broke out of the loop due to low velocity, you still need to disconnect the event you connected earlier
if jumpingEvent ~= nil then
jumpingEvent:Disconnect()
jumpingEvent = nil
end
print("cancelled successfully!")
Essentially this code will temporarily listen to the Humanoid.Jumping event. If that event is triggered, a boolean is set to true so that you can break out of the while-loop. But if the velocity is low enough you also break out of the while-loop.
There are still some edge-cases you might want to iron out down the road. For example, when happens when you slide off the map? What if you die while sliding? Does that cause any errors perhaps? Though this may at least be a solid start that solves your original question.
I would also recommend avoiding the use of a BodyVelocity. If I remember correctly, the LinearVelocity instance is supposed to be its new replacement which should be more up-to-date with more options/features. It is not a big problem right now, but there may be a point in the future when the BodyVelocity instance is no longer being supported.