My slide function is not working

So I want to make it to that when a player slides it will have a lil decline in speed and can stop the slide anytime.

Now sometimes it works and sometimes it doesn’t because it’s still trying to find the linear velocity on my player which causes it to break.

https://gyazo.com/8ce0f19495c3ae62fa5fae7073cb815f

local player = game.Players.LocalPlayer
local Character = script.Parent
local HumanoidRootPart = Character.HumanoidRootPart

local RunS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local SlideAnim = game.ReplicatedStorage.Slide
local AnimTrack = Character.Humanoid.Animator:LoadAnimation(SlideAnim)

local LVAttach = Character.HumanoidRootPart.RootAttachment
local Connection
local Forward = Character.HumanoidRootPart.CFrame.LookVector
local HumanoidRootPart = Character.HumanoidRootPart

local Drag = 0.01
local Force = 60
local Debounce = false
local CD = 1
local Slide = false
local SDelay = false


UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftControl and Debounce == false and Slide == false then
		Slide = true
		Debounce = true
		AnimTrack:Play()
		local LinearVelocity = Instance.new("LinearVelocity", Character.HumanoidRootPart)
		LinearVelocity.Attachment0 = Character.HumanoidRootPart.RootAttachment
		LinearVelocity.MaxForce = 999999
		LinearVelocity.VectorVelocity = Character.HumanoidRootPart.CFrame.LookVector * Force 
		
		task.wait(0.5)
		
		Connection = RunS.Heartbeat:Connect(function(DT)
			SDelay = true
			if HumanoidRootPart.AssemblyLinearVelocity.Magnitude > 0 then
				local dragVector = -HumanoidRootPart.AssemblyLinearVelocity.Unit * HumanoidRootPart.AssemblyLinearVelocity.Magnitude ^ 1.3
				
				LinearVelocity.VectorVelocity += dragVector * Drag
				
			end
		end)
		
		task.wait(2)
		if Character.HumanoidRootPart.LinearVelocity == Character.HumanoidRootPart.LinearVelocity and Slide == true then
			Character.HumanoidRootPart.LinearVelocity:Destroy()
			Slide = false
		else 
			return
		end
		
		Connection:Disconnect()
		SDelay = false
		Slide = false
		AnimTrack:Stop()
		
		wait(CD)
		Debounce = false
	end
end)


UIS.InputEnded:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftControl and Slide == true then
		AnimTrack:Stop()
		Character.HumanoidRootPart.LinearVelocity:Destroy()
		Slide = false
		
		wait(CD)
		Debounce = false
	else
		return
2 Likes

Try to look for LinearVelocity using :WaitForChild()
I think this will fix the problem

1 Like

Your issue here is this.
What if you stop sliding before the 2 seconds? This part of the code will still run, resulting in the code trying to delete the LinearVelocity when it has already been destroyed!

1 Like

I would love to not have the wait there but when I first started this it was set to HumanoidRootPart.AssemblyLinearVelocity.Magnitude = 0 but in the RunService when I slide it never reaches zero and I don’t know the workaround.

I don’t if I’m putting it in wrong but it’s not working.

So how about

task.wait(.5)
		-- "if Character.HumanoidRootPart.LinearVelocity == Character.HumanoidRootPart.LinearVelocity and Slide == true then" this is like saying 'if 1 == 1 and Slide (==true) then', just make it 
        if Character.HumanoidRootPart.LinearVelocity < .1 and Slide then --this checks to see if the LinearVelocity is low (you can change the value higher if necessary) and if Slide is true
			Character.HumanoidRootPart.LinearVelocity:Destroy()
			Slide = false
		else 
			return
		end

[/quote]

1 Like

attempt to compare Instance < number this is the error message
I don’t know if it causes the number is negative but I tried changing it and it didn’t work but i do see what you are saying.

local Character = script.Parent;
local Humanoid = Character:WaitForChild("Humanoid");
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart");

local RunService = game:GetService("RunService")
local UserInputService = game:GetService('UserInputService')

local MapParams = RaycastParams.new();
MapParams.FilterDescendantsInstances = {workspace.Map};
MapParams.FilterType = Enum.RaycastFilterType.Whitelist;


local SlideAnim = game.ReplicatedStorage.Slide
local AnimTrack = Character.Humanoid.Animator:LoadAnimation(SlideAnim)

function GetFloorCast()
	local Raycast = workspace:Raycast(HumanoidRootPart.Position,Vector3.new(0,-10000,0),MapParams)

	return Raycast
end


function GetNewFacial()
	local currentRightVector = HumanoidRootPart.CFrame.RightVector
	local upVector = GetFloorCast().Normal
	local newFacialVector = currentRightVector:Cross(upVector)

	return newFacialVector,upVector,currentRightVector
end

function SetVectorWithDifferntXAndY(Old,NewY)
	return Vector3.new(Old.X,NewY,Old.Z)
end

function StartSlideVelocity()
	local SlideVelocity = Instance.new("BodyVelocity")
	SlideVelocity.MaxForce = Vector3.new(math.huge,100,math.huge)
	SlideVelocity.P = 9e9
	SlideVelocity.Parent = HumanoidRootPart

	local Speed = 60
	AnimTrack:Play()
	while SlideVelocity:IsDescendantOf(workspace) do
		local newFacialVector,upVector,currentRightVector =  GetNewFacial()
		SlideVelocity.Velocity = SetVectorWithDifferntXAndY(HumanoidRootPart.CFrame.LookVector*Speed,-10) 

		if Speed == 0 then
			SlideVelocity:Destroy()
			AnimTrack:Stop()
		end

		if newFacialVector.Y ~= 0 then
			Speed= Speed - (-newFacialVector.Y*3)
		else
			Speed = math.clamp(Speed,0,30)
			if Speed < 0 then
				SlideVelocity:Destroy()
				AnimTrack:Stop()
			end
		end
		Speed-=.1
		Speed = math.clamp(Speed,0,50)
		RunService.RenderStepped:Wait()
	end
	
end

UserInputService.InputBegan:Connect(function(Key,Typing)
	if Typing then
		return
	end
	
	if Key.KeyCode == Enum.KeyCode.LeftControl then		
		StartSlideVelocity()
		
	end
end)

UserInputService.InputEnded:Connect(function(Key,Typing)
	if Typing then
		return end
	if Key.KeyCode == Enum.KeyCode.LeftControl then	
		
		if Character.HumanoidRootPart:FindFirstChild("BodyVelocity") then
			Character.HumanoidRootPart.BodyVelocity:Destroy()
			AnimTrack:Stop()
		else
			return
		end
			
	end		
end)

Alright, I did some more research and come up with this I’ve even put a lil bit extra in but it works now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.