My chunk math isn't aligned how it should be?

Everytime I walk into a new 50 by 50 chunk, it’s supposed to create a part. However, it seems it’s slightly off from where it should be. It’s visible in the video.

There are two problems with my chunk loader:

  1. It doesn’t load the chunk till it’s halfway in it.
  2. It sometimes loads chunks it shouldn’t.
local CurrentChunkPosition = math.floor(HumanoidRootPart.Position.X / 50) .. math.floor(HumanoidRootPart.Position.Z / 50)
	if ChunkPosition ~= CurrentChunkPosition then
		ChunkPosition = CurrentChunkPosition
		local a = Instance.new("Part")
		task.synchronize()
		a.Parent = workspace
		a.CanCollide = false
		a.Anchored = true
		a.Position = Vector3.new(math.floor(HumanoidRootPart.Position.X / 50) * 50, 0, math.floor(HumanoidRootPart.Position.Z / 50) * 50)
		a.Size = Vector3.new(50,50,50)
	end

This is my script. It’s ran in parallel and uses RunService.Heartbeat. I don’t know why it’s not working right. I believe the problem is how I handle chunk alignment, which is written in the first line of the script.

Some things for context:

  1. I won’t change RunService.Heartbeat. It’s used for my anticheat and it’s relatively optimized, under 1% script activity for 8 players. I used Heartbeat so I never miss a frame of character movement.

  2. Please suggest solutions/ideas that don’t involve me creating any sort of parts. I’m trying to make everything serversided and having no impact on the actual game. I really don’t want to use .Touched or any other call that would mean I have to create parts/objects in Workspace.

Any help is appreciated a lot!!

This is simple. The position of the part is in the middle of the part, while you in your code it is considered to be the top left corners position. You need to add +25 to X and Z. to fit it on the chunk.

a.Position = Vector3.new(math.floor(HumanoidRootPart.Position.X / 50) * 50 + 25, 0, math.floor(HumanoidRootPart.Position.Z / 50) * 50 + 25)

This should do.

This is probably a dumb question, but do I have to add 25 to the first line as well?

(This one)

local CurrentChunkPosition = math.floor(HumanoidRootPart.Position.X / 50) .. math.floor(HumanoidRootPart.Position.Z / 50)

Nvm I’m going on only 4 hours of sleep. Sorry for asking this, lol.

No, you don’t need to add that, it’s only cause the Parts Position is in the middle.

You don’t need to be sorry, hope I helped you. Sleep well. :wink: