Help with script to avoid floating point precision error

I am trying to make a script to avoid the floating point precision, i am making a really large game, so it’s really important. It will be a singleplayer game and the script runs in the server, to make life easier.

  • This script works by making the player always close to the origin, teleporting the player back to 0, 0, 0 when too far (In the script it is 10 studs, just for testing) and moving the world to the opposite direction of the player, wich should create a smooth ilusion of the player moving, when in reality the player is stuck inside a box and it is the world that moves.

  • It kinda works, the player and the world are able to move, but the world doesn’t move properly, it is kinda stuttery.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local world = workspace.World --A model that contains every part

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		print("Character loaded")
		RunService.Stepped:Connect(function()
			local oldCFrame = character.HumanoidRootPart.CFrame --Set player CFrame to a var
			if math.abs(oldCFrame.Position.X) >= 10 or math.abs(oldCFrame.Position.Y) >= 10 or math.abs(oldCFrame.Position.Z) >= 10 then
				character:PivotTo(CFrame.new(0, 0, 0) * oldCFrame.Rotation)
				local pivot = world:GetPivot()
				world:PivotTo(pivot - oldCFrame.Position) --Changes model pos to match player pos
			end
		end)
	end)
end)

The key to avoid floating point related errors is to make the map and other players move, not the player himself. That’s where the engine’s limitations start to make things complicated : moving the entire map every frame is exteremly expensive for the client. The only solution I can think of right now is to divide the map into several chunks and move the loaded chunks using local scripts. Unload the chunks which are too far from the closest player to avoid server lag.

3 Likes

At the moment, the world model is only the part and the trees that i showed, so it shouldn’t be too expensive, i am also making it in the server because my game will be singleplayer, i tried using local scripts, but it was more complicated.

1 Like