Slide script not working

Hi, so i was trying to make a slide script in studio it would work but the slide never stops

So i tried using for count 1,8 do but it didn’t work and gave me errors i fixed it using for i=1,8 do that solved the problem but it just made the slide animation never stop how can i possibly fix this
(NOTE: This script was in '‘StarterCharacterScripts’; and It’s a local script)
Here’s the script used:

local UIS= game:GetService("UserInputService")
local char= script.Parent

local slideAnim= Instance.new("Animation")
slideAnim.AnimationId= "rbxassetid://8213794534"

local keybind= Enum.KeyCode.E
local canslide=true

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	if not canslide then return end
	
	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,01)*30000
		slide.Velocity = char.HumanoidRootPart.Cframe.lookVector*100
		slide.Parent = char.HumanoidRootPart
		
		for i=1,8 do 
			wait(0.1)
			slide.Velocity*=0.7
			
		end
		playAnim:Stop()
		slide:Destroy()
		canslide=true 
	end
end)```
local UIS = game:GetService("UserInputService")
task.wait()
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")

local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://8213794534"
slideAnim.Parent = hum
local playAnim = hum:LoadAnimation(slideAnim)

repeat task.wait()
until playAnim.Length > 0

local keybind = Enum.KeyCode.E
local canslide = true

UIS.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if not canslide then return end

	if input.KeyCode == keybind then
		canslide = false
		playAnim:Play()

		local slide = Instance.new("BodyVelocity")
		slide.MaxForce = Vector3.new(math.huge, 0, math.huge)
		slide.Velocity = hrp.CFrame.lookVector*100
		slide.Parent = hrp

		for i = 1, 8 do 
			task.wait(0.1)
			slide.Velocity *= 0.7
		end
		
		playAnim:Stop()
		slide:Destroy()
		canslide = true 
	end
end)

You only need to load the animation which returns an AnimationTrack instance once (then you are free to play/pause/stop the AnimationTrack as you desire). Your main issues were “hrp.Cframe”, the “CFrame” property should be capitalised as such and also when you were calling the Vector3 class constructor which returns a Vector3 value you were missing a comma. One more thing, you should probably parent the animation instance when its created (in the script above I have parented it to the HumanoidRootPart of the player’s character) otherwise its parent is nil.

I’ve also made some other minor improvements/optimisations to the code.

1 Like

Thanks so much and thank you for putting in the effort to help