I made a custom chunk loader that worked before in a separate place. However when I integrated the code into the rest of my game it suddenly breaks. As soon as the player connects the character is deleted for a loading menu. The function
GetCharacter waits for a character to be added when the player spawns in before continuing. However after the 2nd time the
while do loop at the end runs suddenly the whole script yields because it is waiting for a character to be added even though the character already exists which should’ve completely bypassed the if statement.
local function GetCharacter() local Character = Player.Character if not Character or not Character.Parent then Character = Player.CharacterAdded:wait() --point of failure end return Character end local function UpdateChunks() local Character = GetCharacter() local CurrentPosition = AssignPartChunk(Character.Head) --deleted rest of function as not needed end local LastPosition = Vector3.new(0, 0, 0) local LastCurrentXChunk = 0 local LastCurrentZChunk = 0 local ChunkUpdateDebounce = false spawn(function() while wait(0.1) do --loop if ChunkUpdateDebounce == false then local Character = GetCharacter() if Character.Humanoid.Health > 0 and Character:FindFirstChild("Head") then if (Character.Head.Position - LastPosition).magnitude >= ChunkConfig.ChunkUpdateDistance then local CurrentPosition = AssignPartChunk(Character.Head) local CurrentXChunk = CurrentPosition.X local CurrentZChunk = CurrentPosition.Y if LastCurrentXChunk ~= CurrentXChunk or LastCurrentZChunk ~= CurrentZChunk then ChunkUpdateDebounce = true UpdateChunks() LastPosition = Character.Head.Position LastCurrentXChunk = CurrentXChunk LastCurrentZChunk = CurrentZChunk wait(1) ChunkUpdateDebounce = false end end end end end end)