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
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
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
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?