Character disappearing after going away from spawn

Hey there!
I am making 2D Platformer game. I keep facing an issue with the control system, whenever I go too far from spawn, my character just disappears


My camera manipulation local script:

local camera = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer

local rs = game:GetService("RunService")


camera.CameraType = Enum.CameraType.Scriptable

function update()
	
	local character = plr.Character or plr.CharacterAdded:Wait()
	
	if not character then return end
	
	local hrp = character:WaitForChild("HumanoidRootPart")
	
	if not hrp then return end
	
	camera.CFrame = CFrame.new(hrp.CFrame.X, hrp.CFrame.Y, hrp.CFrame.Z) * CFrame.new(-20, 0, 0) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(0))
	
end

rs:BindToRenderStep("Camera", Enum.RenderPriority.Input.Value, update)

My custom movement local script:

local plr = game.Players.LocalPlayer

local rs = game:GetService("RunService")
local cas = game:GetService("ContextActionService")

local jumping = false
local leftVal = 0
local rightVal = 0

function left(actionName, inputState)
	
	local character = plr.Character or plr.CharacterAdded:Wait()
	local humanoid = character:FindFirstChild("Humanoid")
	
	if inputState == Enum.UserInputState.Begin then
		
		leftVal = humanoid.WalkSpeed/16
		
	elseif inputState == Enum.UserInputState.End then
		
		leftVal = 0
		
	end
	
end

function right(actionName, inputState)
	
	local character = plr.Character or plr.CharacterAdded:Wait()
	local humanoid = character:FindFirstChild("Humanoid")
	
	if inputState == Enum.UserInputState.Begin then
		
		rightVal = humanoid.WalkSpeed/16
		
	elseif inputState == Enum.UserInputState.End then
		
		rightVal = 0
		
	end
	
end

function jump(actionName, inputState)
	
	if inputState == Enum.UserInputState.Begin then
		
		jumping = true
		
	elseif inputState == Enum.UserInputState.End then
		
		jumping = false
		
	end
	
end

function update()
	
	local character = plr.Character or plr.CharacterAdded:Wait()
	local hrp = character:WaitForChild("HumanoidRootPart")
	local humanoid = character:FindFirstChild("Humanoid")
	
	if not humanoid then return end
	
	local moveVal = rightVal - leftVal
	
	humanoid:MoveTo(Vector3.new(hrp.CFrame.X, hrp.CFrame.Y, hrp.CFrame.Z + moveVal))
	
	if jumping then
		
		humanoid.Jump = true
		
		if leftVal == 0 and rightVal == 0 then return end
		
		local linVel = Instance.new("LinearVelocity", hrp)
		linVel.Attachment0 = hrp.VelAttachment
		
		if leftVal > rightVal then
			
			linVel.LineDirection = Vector3.new(0, 0, -1)
			
		elseif rightVal > leftVal then
			
			linVel.LineDirection = Vector3.new(0, 0, 1)
			
		end
		
		linVel.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
		linVel.LineVelocity = 30
		
		wait(0.1)
		
		linVel:Destroy()
		
	end
	
end

rs:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, update)

cas:BindAction("Left", left, true, "a", Enum.KeyCode.A, Enum.KeyCode.DPadLeft)
cas:BindAction("Right", right, true, "d", Enum.KeyCode.D, Enum.KeyCode.DPadRight)
cas:BindAction("Jump", jump, true, " ", Enum.KeyCode.Space, Enum.KeyCode.DPadUp)```
1 Like

Seems like your video isn’t working… We can’t help you out until that’s fixed. (Unless we just download the video, still would advise you to fix that)

From what I can assume, it might be that the character’s velocity is reaching math.huge? Do you experience some input delay when it was happening? You could check the RootPart’s velocity to make sure it’s not reaching a very high velocity, which would result in the character just disappearing.

1 Like

I checked the velocity and it does reach huge numbers like 1.7809204835134672e-14, but I don’t know how to fix it

try putting a limit on the velocity for example if the velocity is bigger than 100 set it to 100

I am trying to do this in a while loop, is that fine? Because it doesn’t work when the player is idle
Here’s my script:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

while wait(0.3) do
	
	local hrp: Part = char:WaitForChild("HumanoidRootPart")
	
	local velocity = hrp.AssemblyLinearVelocity
	
	if velocity.X > 100 then
		
		hrp.AssemblyLinearVelocity = Vector3.new(100, 0, 0)
		
	elseif velocity.X < 0 then
		
		hrp.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		
	end
	
	if velocity.Z > 100 then
		
		hrp.AssemblyLinearVelocity = Vector3.new(0, 0, 100)
		
	elseif velocity.Z < 0 then
		
		hrp.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		
	end
	
	print(velocity)
	
end```

It’s something to do with the linear velocity, I believe. You probably should have a variable to check if the player had already jumped, with a boolean variable. Maybe the sheer amount of LinearVelocitys you’re creating within the RenderStep connection is what’s causing the huge amount of velocity. Not saying this is the solution, but you could try out that theory by simply not creating the LinearVelocity. Why do you need the LinearVelocity anyway?

I needed the LinearVelocities because my custom movement system won’t allow the player to even cross a jump of 5 studs

Well, firstly, try to run the game without instancing a LinearVelocity to see if the problem still persists. I don’t want to have you redo something that might’ve not been what’s causing the conflict.

Yup, the problem still persists. I commented out the LinearVelocity part in the script, didn’t fix the problem. The velocity is still like 9.360414986923872e-16
And Note: I did comment the Instance.new()