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```
Just a few simple questions to try troubleshoot the problem.
What does your localscript look like?
Is LinearVelocity replaced every time your character is added? (Destroyed on death, add on character load).
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.
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