Issue with Linear Velocity

I am trying to use LinearVelocity for character movement and put it into a module script but anytime I reset the character is unable to move, this issue doesn’t occur if I put it in a local script but I am going to use this function alot so that isn’t an option, I also tried to add characteradded event but for some reason it makes a few values become nil

local MovementMech = {}

local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")

local Player = PlayerService.LocalPlayer
local Character = Player.Character or PlayerService:WaitForChild("Character")
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local RootAttachment = Root:WaitForChild("RootAttachment")
MovementMech.maxspeed = 40
MovementMech.acceleration = 0.5
MovementMech.deacceleration = 2.1


local LinearVelocity = Root:WaitForChild("LinearVelocity")

function MovementMech.OnMoving(_, dt)
	if LinearVelocity then
		local Character = Player.Character or PlayerService:WaitForChild("Character")
		MovementMech.movedirection = Humanoid.MoveDirection
		MovementMech.targetvelocity = Vector2.new(MovementMech.movedirection.X, MovementMech.movedirection.Z)*MovementMech.maxspeed
		MovementMech.deltavelocity = MovementMech.targetvelocity - LinearVelocity.PlaneVelocity
		MovementMech.accelerationrate = if MovementMech.movedirection.Magnitude > 0 then MovementMech.acceleration else MovementMech.deacceleration

		LinearVelocity.PlaneVelocity += MovementMech.deltavelocity * MovementMech.accelerationrate * dt
	end
end


RunService.Stepped:Connect(MovementMech.OnMoving)


return MovementMech```
1 Like

Where’s your script placed in?

(StarterPack, StarterPlayerScripts, StarterCharacterScripts, etc)

1 Like

Both the module and the script requiring it are in StarterCharacterScripts.

I see. Have you tried disconnecting the RunService after death?

i tried doing that but same issue occurs, also i tried printing inside OnMoving but it stops completely when the player dies

local connection = RunService.Stepped:Connect(MovementMech.OnMoving)

Player.CharacterAdded:Connect(function()
	connection:Disconnect()
end

aswell as

local connection = RunService.Stepped:Connect(MovementMech.OnMoving)

Humanoid.Died:Connect(function()
	connection:Disconnect()
end)

Just a few simple questions to try troubleshoot the problem.

  1. What does your localscript look like?

  2. Is LinearVelocity replaced every time your character is added? (Destroyed on death, add on character load).

  3. Are you declaring the new character when you respawn and when you run the function? I’m just wondering, since you have “Player = PlayerService.LocalPlayer”, and no parameters to check for a new player/character instance.

1 Like

what does the local script look like where you call this function and what not?

sorry for the late reply but i found the problem
i had to put
lua local Movement = require(script:WaitForChild("MovementPhysics"))
in my local script instead of directly requiring it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.