There's a chance that when I use Player:LoadCharacter(), I load -3.4028234663852886e+38 studs below the map

  1. What do you want to achieve?
    I’m trying to make it so when the player chooses a weapon, the server loads their character.

  2. What is the issue?
    There’s a random chance that when I load, the character just goes about… -3.4028234663852886e+38 studs below the world??

  3. What solutions have you tried so far?
    I’ve searched far and wide and I found a single topic related to this… from 2015.
    I also have a top-down camera script which, well, makes the game have a top-down view, and also rotates the HumanoidRootPart towards the mouse. Could it be related to that?
    And I tried changing the character’s position to somewhere it can spawn, but it gives the same results.

I have no idea why this is happening, is it related to the said camera script?

Script
--// Services //--
local RunService	=	game:GetService("RunService")
local Players		=	game:GetService("Players")



--// Variables //--

--// Main
local Player		=	Players.LocalPlayer
local Camera		=	workspace.CurrentCamera
local Mouse			=	Player:GetMouse()
local Character		=	Player.Character
local Humanoid		=	Character:WaitForChild("Humanoid")
local RootPart		=	Character:WaitForChild("HumanoidRootPart")
Camera.CameraType	=	Enum.CameraType.Scriptable
Mouse.TargetFilter	=	Character



--// Functions //--
local StartPos = RootPart.Position
Camera.CFrame = CFrame.new(StartPos,Vector3.new(StartPos.X,0,StartPos.Z))

RunService.RenderStepped:Connect(function(DeltaTime)
	if Humanoid and Humanoid.Health > 0 and RootPart then
		local HitPos	=	Mouse.Hit.Position
		local RootPos	=	RootPart.Position
		
		RootPart.CFrame	=	CFrame.new(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z))
		Camera.CFrame	=	Camera.CFrame:Lerp(CFrame.new(Vector3.new(RootPos.X-1,32,RootPos.Z),Vector3.new(RootPos.X,0,RootPos.Z)),1-0.04^DeltaTime)
	end
end)

Thanks for reading :slight_smile:

Maybe what you could do is after Loading the Character set the Character HumanoidRootPart Position to the Spawn location or whatever your spawn is.

1 Like

Doesn’t work, forgot to mention I tried it.

How does it not work? Like does it teleport you or it errors?

Might be a LoadCharacter Bug Not sure.

It only teleports, I tried using a BindToRenderStepped() function to check the position, and when it happens it just stays there.

My thoughts are its most likely related to this. Occasionally, a mathematical error can occur which causes a value to become an extremely unrealistic number. If its a case of nan, you can check for this by comparing a value to itself.

if value1 == value1 then
   --number is not NaN, set RootPart Cframe now.
else
   --number is nan, dont set the cframe.
end

Below is an example of me creating a CFrame with a NaN value, then applying it to a part.

As you can see, it produces the same number you’re seeing on your end when applied to the part.

2 Likes

Doesn’t seem to be working, maybe I wrote it wrong?

local RootCFrame = CFrame.new(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z))
if RootCFrame.Position.Y == RootCFrame.Position.Y then
	RootPart.CFrame = RootCFrame
end

That’s something new I learned at least hah

Positions cannot contain NaN, so when you do that you’re essentially just asking if -3 == -3.

You have to compare the value that is created before it gets applied to the character.

ex:

RunService.RenderStepped:Connect(function(DeltaTime)
	if Humanoid and Humanoid.Health > 0 and RootPart then
		local HitPos	=	Mouse.Hit.Position
		local RootPos	=	RootPart.Position
		
        local CFrameToApply = CFrame.lookAt(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z)) -- .new() in this way was deprecated in favor of .lookAt()
        if CFrameToApply.Position ~= CFrameToApply.Position then
           return
        end

		RootPart.CFrame	=	CFrameToApply
		Camera.CFrame	=	Camera.CFrame:Lerp(CFrame.new(Vector3.new(RootPos.X-1,32,RootPos.Z),Vector3.new(RootPos.X,0,RootPos.Z)),1-0.04^DeltaTime)
	end
end)

Still doesn’t seem to work…

local RootCFrame = CFrame.new(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z))	
if RootCFrame.Position ~= RootCFrame.Position then
	return
end
RootPart.CFrame = RootCFrame

Err, oh wait, I didn’t see the .lookAt!
… Nevermind, still doesn’t work.

The problem may not be there then, it’s possible that another script is setting the position of the part to NaN. Have you tried disabling the camera script and seeing if the character still gets stuck?

Yeah, when I remove the line where it rotates the RootPart, it seems to be working fine.
Is there maybe another way I could make it rotate? Body movers perhaps?
Or maybe I should use a new way of checking the mouse 3D position with the UserInputService?

Interesting, if you can’t pick up on NaN for some reason you may be able to avoid it by comparing the magnitude of the 2 positions beforehand.

Take a look at this line of code:

print(CFrame.lookAt(Vector3.new(), Vector3.new()))

This will produce 0, 0, 0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN because both values have a magnitude of 0.

So if you were to compare the position of this value to itself via ==, it should be false. Assuming this doesn’t work for some reason, you can check if the 2 positions have a magnitude < a number (ex: 0.1)

if (HitPos - RootPos).Magnitude < 0.1 then
   return
end

Comparing a number to the magnitude doesn’t work too, and my brain is melting rn.
I literally checked if the values were NaN, it said they’re not, and it still applied it…???
image

local Mag = (HitPos - RootPos).Magnitude
print(Mag)	
if Mag ~= Mag then
	print(Mag,"FOUND IT")
	return
end
RootPart.CFrame	=	CFrame.lookAt(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z),RootPart.CFrame.UpVector)

I mean, the character still got destroyed, this is really weird…

I saw someone doing something like this:

if Pos.X == Pos.X and Pos.Y == Pos.Y and Pos.Z == Pos.Z then

I’ll try it but I have to sleep, still though thanks a ton for the help :))

Try this out and let me know what happens, I’m curious as to what specifically is happening.

--// Services //--
local RunService	=	game:GetService("RunService")
local Players		=	game:GetService("Players")



--// Variables //--

--// Main
local Player		=	Players.LocalPlayer
local Camera		=	workspace.CurrentCamera
local Mouse			=	Player:GetMouse()
local Character		=	Player.Character
local Humanoid		=	Character:WaitForChild("Humanoid")
local RootPart		=	Character:WaitForChild("HumanoidRootPart")
Camera.CameraType	=	Enum.CameraType.Scriptable
Mouse.TargetFilter	=	Character



--// Functions //--
local StartPos = RootPart.Position
Camera.CFrame = CFrame.new(StartPos,Vector3.new(StartPos.X,0,StartPos.Z))

RunService.RenderStepped:Connect(function(DeltaTime)
	if Humanoid and Humanoid.Health > 0 and RootPart then
		local HitPos	=	Mouse.Hit.Position
		local RootPos	=	RootPart.Position
		
        local Magnitude = (HitPos - RootPos).Magnitude
        if Magnitude < 0.001 or Magnitude ~= Magnitude then
            print("Found invalid values, will not set CFrame")
        else
            RootPart.CFrame	=	CFrame.new(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z))
		    Camera.CFrame	=	Camera.CFrame:Lerp(CFrame.new(Vector3.new(RootPos.X-1,32,RootPos.Z),Vector3.new(RootPos.X,0,RootPos.Z)),1-0.04^DeltaTime)
        end
	end
end)
1 Like

It printed “Found invalid values, will not set CFrame”, yet still applied it for some reason…
Maybe just like you said, if a position becomes NaN it will replace it with a weird number?

Once, I had problem with CFrame.new(LookPos, LookAt) too. This tutorial helped me a lot:

1 Like

For a second I thought it was the solution, but I kept resetting and it still occured…

local function GetCFrame(Position,LookAt)
	local LookVector	=	(Vector3.new(LookAt.X,0,LookAt.Z) - Vector3.new(Position.X,0,Position.Z)).Unit
	local ModelUpVector	=	Vector3.new(0,1,0)
	local RightVector	=	LookVector:Cross(ModelUpVector)
	local UpVector		=	RightVector:Cross(LookVector)
	return CFrame.fromMatrix(Position,RightVector,UpVector)
end

local StartPos = RootPart.Position
Camera.CFrame = CFrame.new(StartPos,Vector3.new(StartPos.X,0,StartPos.Z))

RunService.RenderStepped:Connect(function(DeltaTime)
	if Humanoid and Humanoid.Health > 0 and RootPart then
		local HitPos	=	Mouse.Hit.Position
		local RootPos	=	RootPart.Position
		
		RootPart.CFrame	=	GetCFrame(RootPos,HitPos)
		--RootPart.CFrame	=	CFrame.lookAt(RootPos,Vector3.new(HitPos.X,RootPos.Y,HitPos.Z))
		Camera.CFrame	=	Camera.CFrame:Lerp(CFrame.new(Vector3.new(RootPos.X-1,32,RootPos.Z),Vector3.new(RootPos.X,0,RootPos.Z)),1-0.04^DeltaTime)
	end
end)

Is that entire script? If something caused it to set different number…

There are no other scripts that cause it to become NaN, though now when I’m testing it, it seems to occur less.
I’m really confused, maybe I should send the place file?

Okay I have a theory…
I removed the lines where the RootPart rotates towards the mouse and I used an autoclicker to reset myself whenever I press Esc + R.
It seems that the thing still occurs, so my theory is that maybe IT IS related to another script?
When the player dies, the server sets their character to nil and destroys it.
So maybe when the player dies, the event overlaps with the new character somehow?

Event_ChooseWeapon.OnServerEvent:Connect(function(Player,Weapon)
	if not Player.Character and Weapon then
		local WeaponModel = Fold_Weapons:FindFirstChild(Weapon)
		if WeaponModel then
			Player:LoadCharacter()
			print("SPAWNED")
			
			local Character	=	Player.Character			
			local Humanoid	=	Character.Humanoid
			local RootPart	=	Character.HumanoidRootPart
			
			Humanoid.Died:Connect(function()
				print("DESTROYED")
				Player.Character = nil
				Character:Destroy()
				Event_DisplayDeath:FireAllClients(RootPart.Position)
			end)

Nevermind, I removed the Character:Destroy() and the RootPart still kept being NaN.