How would I make my function stop after I finish sliding?

In this script, I am making the player slide. This includes the players angle changing (adapting to the floor) and when the slide is over, the player returns to having no adaption to the terrain (not adapted, therefore no player rotation to match surface)

no matter how many things i’ve tried, all of it has failed. Maybe it is because its late and I am not thinking right, but I came here as kind of a last resort. The main issue is me not knowing how to turn off functions. I’ve tried using return and :Destroy, but non of them have been working.

The rest of the script works, just not the function part, I need it to end somehow but I just cant wrap my head around it this time. Literally any help is appreciated…

What the script does now (Visual)
^Problem is that it should stop adjusting the character to slopes once the slide is over.

Full script:

*scroll down and across to see the whole script

local UIS = game:GetService("UserInputService")
local character = script.Parent
local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://9891447905"

local KeyBind = Enum.KeyCode.LeftControl
local Slidable = true

UIS.InputBegan:Connect(function(input,gameprocessed)	
	
	local function SlideAngle()

		local char = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
		local lowerTorso, rootPart = char:WaitForChild("LowerTorso"), char:WaitForChild("HumanoidRootPart")

		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {char}
		params.FilterType = Enum.RaycastFilterType.Blacklist

		game:GetService("RunService").Heartbeat:Connect(function()

			local ray = workspace:Raycast(rootPart.Position, Vector3.new(0, -char.Humanoid.HipHeight - (rootPart.Size.Y / 2) - 2, 0), params)
			if ray then

				local vector = rootPart.CFrame:VectorToObjectSpace(ray.Normal)
				lowerTorso.Root.C0 = CFrame.new(0,-1,0,1,0,0,0,1,0,0,0,1) * CFrame.Angles(vector.z, 0, -vector.x)
			end
		end)
	end
	
	if gameprocessed then return end
	if not Slidable then return end
	
	if input.KeyCode == KeyBind then
		
		Slidable = false
		SlideAngle()
		local playAnim = character.Humanoid:LoadAnimation(slideAnim)
		playAnim:Play()
		
		local Brown = Color3.new(0.635294, 0.584314, 0.623529)
		local Bronze = Color3.new(0.658824, 0.627451, 0.643137)
		local Particle = Instance.new("ParticleEmitter")
		Particle.Color = ColorSequence.new{
			ColorSequenceKeypoint.new(0, Brown),
			ColorSequenceKeypoint.new(1, Bronze)
		}
		Particle.Parent = character.Humanoid.LeftLeg
		Particle.Lifetime = NumberRange.new(0.5,0.6)
		Particle.Transparency = NumberSequence.new(0.4,0.5)
		Particle.Rate = 1000
		Particle.SpreadAngle = Vector2.new(50,50)
		Particle.Speed = NumberRange.new(0.1,0.1)
		Particle.Texture = "rbxasset://textures/particles/smoke_main.dds"
		Particle.Acceleration = Vector3.new(0,3,0)
		Particle.Rotation = NumberRange.new(5,5)
		Particle.RotSpeed = NumberRange.new(5,5)

		local Brown = Color3.new(0.635294, 0.584314, 0.623529)
		local Bronze = Color3.new(0.658824, 0.627451, 0.643137)
		local ParticleRight = Instance.new("ParticleEmitter")
		Particle.Color = ColorSequence.new{
			ColorSequenceKeypoint.new(0, Brown),
			ColorSequenceKeypoint.new(1, Bronze)
		}
		ParticleRight.Parent = character.Humanoid.RightLeg
		ParticleRight.Lifetime = NumberRange.new(0.5,0.6)
		ParticleRight.Transparency = NumberSequence.new(0.5,0.6)
		ParticleRight.Rate = 1000
		ParticleRight.SpreadAngle = Vector2.new(50,50)
		ParticleRight.Speed = NumberRange.new(0.1,0.1)
		ParticleRight.Texture = "rbxasset://textures/particles/smoke_main.dds"
		ParticleRight.Acceleration = Vector3.new(0,3,0)
		ParticleRight.Rotation = NumberRange.new(5,5)
		ParticleRight.RotSpeed = NumberRange.new(5,5)
		
		local slide = Instance.new("BodyVelocity")
		slide.MaxForce = Vector3.new(1,0,1) * 28000
		slide.Velocity = character.HumanoidRootPart.CFrame.lookVector * 100
		slide.Parent = character.HumanoidRootPart
		character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0,0.001,0) * CFrame.Angles(math.rad(0.001),0.001,0.001)
		
		for count = 1, 8 do
			
			wait(0.1)
			slide.Velocity *= 0.7
			
		end
		SlideAngle():Destroy()
		playAnim:Stop()
		slide:Destroy()
		Particle:Destroy()
		ParticleRight:Destroy()
		wait(2.5)
		Slidable = true 
		end
	
end)

the main issue is on line 89, and lines 11-29.

I am so tired and im sorry if this is a stupid question, I am just really at a loss for what I can do, I’ve looked at the developer roblox page about functions too, but nothing works.

thanks again for your help, I’ll wake up and read the answers (hopefully)
-Snipperdiaper

This might have a somewhat easy fix, however you may need to restructure a bit. You have on line 20 used :Connect(), if you stored this inside a variable:

local heartbeat = game:GetService("RunService").Heartbeat:Connect(function()
	...
end)

You may then call heartbeat:Disconnect() and it will cease to react on heartbeat until it is connected again.

2 Likes