Chunk Loader not working

I created a chunk loader / unloader for my game, but it has an issue. It only runs once even though I put it in a while true do loop. Here is the code:


local e = {}
local c = 50
while true do
	game:GetService("RunService").Heartbeat:Wait()
	for a, b in pairs(workspace.Blox:GetChildren()) do
		print("Hmm")
		if script.Parent:FindFirstChild("HumanoidRootPart") then
			local a = (script.Parent:FindFirstChild("HumanoidRootPart").Position - b.Position).Magnitude
			if a > c then
				table.insert(e, b)
				b.Parent = nil
			end
		end
	end
	for b, d in pairs(e) do
		if script.Parent:FindFirstChild("HumanoidRootPart") then
			local a = (script.Parent:FindFirstChild("HumanoidRootPart").Position - d.Position).Magnitude
			if a <= c then
				table.remove(e, b)
				d.Parent = workspace
			end
		end
	end
end

Could someone tell me how to fix this?

I just now realized that I have made a topic similar to this one, however this is a different issue I am facing

1 Like

Is this code Clientside or Serverside?

1 Like

ClientSide

Sorry for the late response.

Don’t worry, I have time anyways.

Ok, first of all, could we have a screenshot of your project structure? (Screenshot of your Explorer)
Second of all, is there anything in the output? (If possible, also provide a screenshot of the Output)

Edit: I just noticed, is the LocalScript inside of the Character? If yes, then the problem is there. Let the LocalScript be in a location like StarterPlayerScripts, you can get the Player’s Character always by doing this:

game:GetService("Players").LocalPlayer.Character

But don’t forget to check if the Player has a character, by doing

if Character then
 -- Rest of your code
end
1 Like

Why would that cause a problem if it is in the character lmao? Shouldn’t they run the same no matter the parent?

Make a folder in the workspace and put the objects in there (Make sure they are not locked :P).

Nothing in the output when I run it.

The issue is that your LocalScript is located in Workspace, a location it will not run in.

Taking this back, it’s in the Player’s Character. I’m assuming that not even Hmm gets printed out? Or if it does, how many times does it get printed out?

Edit2: I think your problem is in the second pairs loop. You are setting the parent of the parts to Workspace, not your Blox folder.

	for b, d in pairs(e) do
		if script.Parent:FindFirstChild("HumanoidRootPart") then
			local a = (script.Parent:FindFirstChild("HumanoidRootPart").Position - d.Position).Magnitude
			if a <= c then
				table.remove(e, b)
                                -- V here is the error I suspect
				d.Parent = workspace
			end
		end
	end
2 Likes

Once

30 CharacterZerererrrrrrwwwww

First, did you try my possible solution above regarding your parent situation.
Second, is there only one child inside of the Blox folder when it prints or multiple?

1 Like

It works but when the player falls out of the map, and into the “void” it does not.

Only one child

@SimplyData

Ok, did you change .Parent = workspace to .Parent = workspace.Blox, and what is the code that generates the parts?

1 Like

Your solution seemed to fix my problem.

No more further replies needed.