Problems with sliding system

So I’m trying to make a sliding system and I have made pretty good progress.

The script would work perfectly if it wasn’t for the fact that you can maintain yourself in the air if you slide, instead of falling down to the floor. This is a problem because I plan my game to have slopes., and if you slide before one, you will just fly over it.

This is the script:

local ts = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")

local crouch = Enum.KeyCode.C
local run = Enum.KeyCode.LeftShift

local running = false

local tween = ts:Create(script.Parent.HumanoidRootPart, TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.In), {Velocity = Vector3.new(0, 0, 0)})

local anim = script.Parent:WaitForChild("Humanoid"):LoadAnimation(script.slide)

uis.InputBegan:Connect(function(key_)
	
	if key_.KeyCode == crouch then
		if running == true then
			anim:Play(0)
			script.Parent.HumanoidRootPart.Velocity = script.Parent.HumanoidRootPart.CFrame.LookVector * 35
			tween:Play()
			ts:Create(script.Parent.Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {CameraOffset = Vector3.new(0, -3, 0)}):Play()
			ts:Create(script.camtilt, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Value = -15}):Play()
		end
	end
	
	if key_.KeyCode == run then
		running = true
		script.Parent.Humanoid.WalkSpeed = 35
	end
	
end)

uis.InputEnded:Connect(function(key_)
	
	if key_.KeyCode == crouch then
		anim:Stop()
		tween:Cancel()
		ts:Create(script.Parent.Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {CameraOffset = Vector3.new(0, 0, 0)}):Play()
		ts:Create(script.camtilt, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Value = 0}):Play()
	end
	
	if key_.KeyCode == run then
		running = false
		script.Parent.Humanoid.WalkSpeed = 18
	end
	
end)

rs.RenderStepped:Connect(function()
	
	if (script.Parent.HumanoidRootPart.Velocity).Magnitude < 2 then
		anim:Stop()
		tween:Cancel()
		ts:Create(script.Parent.Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {CameraOffset = Vector3.new(0, 0, 0)}):Play()
		ts:Create(script.camtilt, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Value = 0}):Play()
	end
	
	game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.Angles(0, 0, math.rad(script.camtilt.Value))
	
end)

I know the problem is that I set the HumanoidRootPart’s velocity in a way that the velocity in the Y axis doesn’t change until the slide is over, I just don’t know how to make the Y velocity behave as normal while also messing with the X and Z velocity.

Thanks for the help in advance!

One alternative to this is checking if the player is jumping or free falling before running the slide animation. You can do this by using the function:

local humanoid = script.Parent.Humanoid -- setting a variable for humanoid is a good idea even if you don't use this mechanism, since you reference it many times
local currentState = humanoid:GetState()
if currentState == Enum.HumanoidStateType.Jumping or currentState == Enum.HumanoidStateType.Freefall then
	-- stop animation from running
end

The con to this is that players cannot use the animation when in the air, and I’m not sure the intentions of your game, so this may not be aimed at your objectives. Feel free to disregard this system.

I thought of doing something like that, but if I went over a slope while sliding, the slide would just stop. What I need is the slide to still go forward, but get the Y velocity to behave as if I wasn’t sliding.

You can probably get away with adding to the velocity instead of explicitly setting it (replace the .Velocity = ... with .Velocity += ...).

I can do that too, but it doesn’t fix the problem. Now that I realize, the problem is that when tweening the velocity to zero, it also tweens the Y velocity. All that I would need to fix it would be to be able to tween the X and Z velocity without modifying the Y velocity, but I don’t know if I can do that. Also, searching for solutions I saw that Velocity was deprecated, so I’m using AssemblyLinearVelocity now. For what I know, it’s basically the same.

I’m going to use WalkSpeed for now. I will revert the system to AssemblyLinearVelocity if I get to fix the script.