Any way to improve my movement code?

So I’m currently making a game about racing, and I made a simple script that fires everytime a player moves and It does 3 things:

  • Increase/Decrease the player speed until It reaches the max/min speed.
  • Change the player’s character physical properties so It slips.
  • Enable a “smoke” particle effect when the humanoid speed is low (When the humanoid accelerates or slows down).

I feel that I can improve the code a lot in many different ways, but I already scripted 2 times the code from scratch and still didn’t made so much improvement.

local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService") --This service is used for other part of the code, ignore it.
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Players = game:GetService("Players")

local MovementValues = ServerStorage:WaitForChild("MovementValues", 100)
local PlayerEvents = ReplicatedStorage:WaitForChild("PlayerEvents", 100)

local DefaultSpeed = MovementValues.DEFAULT_SPEED.Value --(60)
local SmokeSpeed = MovementValues.SMOKE_SPEED.Value     --(45)
local MaxSpeed = MovementValues.MAX_SPEED.Value         --(70)

local function RunningSmoke(Character, State)
	local RightLeg = Character:WaitForChild("Right Leg", 5)
	local LeftLeg = Character:WaitForChild("Left Leg", 5)
	RightLeg.RunSmoke.RunSmoke.Enabled = State
	LeftLeg.RunSmoke.RunSmoke.Enabled = State

local function RootPartPhysics(Hrp, Density)
	Hrp.CustomPhysicalProperties =, 0, 0, 50, 0)

game.ReplicatedStorage.PlayerEvents.PlayerMove.OnServerEvent:Connect(function(Player, Character, Humanoid, Hrp, IsMoving)
	if (not Character or not Humanoid or not Hrp or Humanoid:GetState() == Enum.HumanoidStateType.Dead) then
	local BodySpeed = Hrp.Velocity.Magnitude
	if (IsMoving >= 1 and Humanoid.WalkSpeed <= MaxSpeed) then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed + .1
	elseif (IsMoving <= 0 and Humanoid.WalkSpeed >= DefaultSpeed or BodySpeed < Humanoid.WalkSpeed and Humanoid.WalkSpeed >= DefaultSpeed) then
		RootPartPhysics(Hrp, .4)
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - .5
	if (IsMoving >= 1 and BodySpeed <= SmokeSpeed) then		
		RunningSmoke(Character, true)
		RootPartPhysics(Hrp, .4)
	elseif (IsMoving <= 0 or BodySpeed >= SmokeSpeed or Humanoid.WalkSpeed >= (MaxSpeed - 5)) then	
		RunningSmoke(Character, false)
		RootPartPhysics(Hrp, .8)

If my explanation was so bad, you can check the place where I’m doing this so you can test what the code does.

1 Like

I think you may be sending too much information through the remote event. Like if the player is being sent through the event, you can just check character, humanoid, and hrp from there rather than having it also being sent (correct me if I’m wrong).

Also what is “IsMoving”?

My other suggestion is maybe just have DefaultSpeed, SmokeSpeed, and MaxSpeed as local variables in the script so it’s just quicker to change in the future if need be.

1 Like

IsMoving it’s a value that determinates if the player is moving or not.

IsMoving >= 1 (true)

IsMoving < 0 (false)

1 Like

I tried the game out and I think it works great, don’t see much else you need to change. The only thing maybe to look at is how often the remote event is fired but that is something I can’t speculate on.