Light-Speed dash help

Hello! I’m trying to make a light-speed dash script as seen in the Sonic The Hedgehog games but my current script is really janky and I’m a beginner scripter so does anyone know any solutions to make my script run smoother?

LightSpeed.lua (1.8 KB)

Could you show us the script without needing to download it?

2 Likes
--|Services|--

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local WS = game:GetService("Workspace")
local RS = game:GetService("RunService")
local RCS = game:GetService("ReplicatedStorage")
local CAS = game:GetService("ContextActionService")

--|Player|--

local Player = Players.LocalPlayer
local Char = Player.Character
local Hum = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")

--|Variables|--

local CanLightDash = true
local LightDashing = false
local oof
local LSDFolder = WS.SEObjects.Collectables.Rings.LightSpeedDash
local BP

--|Animation|--

local LSDanim = Hum:LoadAnimation(RCS:WaitForChild("Controls"):WaitForChild("LightSpeedDash"))

--|Main|--

local function LightSpeedDashFunction()
	if Enum.UserInputState.Begin then
		oof = RS.Heartbeat:Connect(function()
			for _, ring in pairs(LSDFolder:GetDescendants()) do
			if ring:IsA("Part") and ring.Name == "ring" then
					local ringmag = (HRP.Position - ring.Position)
					if ringmag.Magnitude <= 4.5 then
						LightDashing = true
						CanLightDash = false
						LSDanim:Play()

						HRP.CFrame = ring.CFrame

						HRP.Velocity = ring.CFrame.lookVector
					else
						LSDanim:Stop()
						LightDashing = false
						CanLightDash = true
					end
				end
			end
		end)
	elseif Enum.UserInputState.End and LightDashing == true then
		oof:Disconnect()
		LSDanim:Stop()
	end
end

Hum.StateChanged:Connect(function(oldstate, newstate)
	if newstate == Enum.HumanoidStateType.Landed and LightDashing == true then
		oof:Disconnect()
		LSDanim:Stop()
	end
end)

CAS:BindAction("LightSpeed", LightSpeedDashFunction, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonY)
CAS:SetImage("LightSpeed", "rbxassetid://9831919760")
CAS:SetPosition("LightSpeed", UDim2.new(0.65, 0,0.8, 0))