Game Breaking Because Of RunService

I’ve made a system that uses :ApplyImpulse, to move a part called “CharacterController” which moves the players character

But the problem is for some reason the whole game starts glitching and the CharacterController just disappears. Also, when I comment out the code inside the function but not the function it doesn’t glitch anymore.

Heres the code:

	RunService.RenderStepped:Connect(function(delta)
		if self.MoveVector ~= Vector3.zero then
			self.MoveVector = Vector3.new(self.Humanoid.MoveDirection.X, 0, self.Humanoid.MoveDirection.Z)
			self.MoveDirection = Vector3.new(self.Humanoid.MoveDirection.X, 0, self.Humanoid.MoveDirection.Z) * (fps * delta) * self.Humanoid.Property.WalkSpeed
			self.Humanoid.CharacterController:ApplyImpulse(self.MoveDirection * self.Humanoid.Character.HumanoidRootPart.AssemblyMass)
			self.Humanoid.CharAlignOrientation.CFrame = CFrame.lookAt(Vector3.zero, self.MoveVector)
		end
	end)

the issue is most likely due to CFrame.lookAt() getting 2 of the same vectors (both 0,0,0) resulting in “nan” orientation, which has a habit of breaking graphics

simply make sure that self.MoveVector is not zero

Thanks I commented out the part where it says CFrame.lookAt and it worked, but I’m wondering how I could add the cframe.lookAt stuff back without it breaking the code

try adding a simple if statement:

if self.MoveVector.Magnitude ~= 0 then
	self.Humanoid.CharAlignOrientation.CFrame = CFrame.lookAt(Vector3.zero, self.MoveVector)
end
1 Like

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