How can I improve this velocity script?

Im making a game like Super Mario 64, and so I wanted to implement a velocity script that would make the player run faster while there running overtime, I want to find ways that I can improve this script, whether it be for increasing player performance or fixing flaws and or exploits / ways players can abuse this script.

local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")

local root = script.Parent:WaitForChild("HumanoidRootPart")

local UserInputService = game:GetService('UserInputService')
local KEYS = {Enum.KeyCode.W, Enum.KeyCode.S, Enum.KeyCode.A, Enum.KeyCode.D}

local function isKeyDown()
	for _, key in KEYS do
		if UserInputService:IsKeyDown(key) then
			return true
		end
	end
	return false
end

while task.wait() do
	local result = isKeyDown()
	if humanoid then
		task.wait(3.3)
		if humanoid.WalkSpeed == 16 then
			if result then
				for i = 1, 8 do
					if humanoid.WalkSpeed == 32 then
						break
					end

					task.wait(0.008)
					humanoid.WalkSpeed += 2
				end
			end
		end

		if not result then
			for i = 1, 8 do
				if humanoid.WalkSpeed == 16 then
					break
				end

				task.wait(0.008)
				humanoid.WalkSpeed -= 2
			end
		end
	end
end
1 Like

I would use <= 16 and >= 32. So if Speed value goes below or above the limit it would just go crazy values.

1 Like

This is a LocalScript, but WalkSpeed is changed from it, so that part should be moved to a server side script.

Also, there is a wait 3.3 seconds every loop. Iā€™m not sure if that is intentional, but I believe it might feel sluggish to players? What is the goal of the wait?

Also, you have loops that change the WalkSpeed, maybe those could be turned into tweens? (TweenService)

Also, purely readability, but this is redundant

if humanoid.WalkSpeed == 16 then
	if result then
      -- code
  end
end
if not result then
  -- code
end

two checks are made for the value of result, this can be simplified:

if result and humanoid.WalkSpeed == 16 then
    -- code
else -- equivalent to "if not result then"
  -- code
end