So, i’ve been trying to write this code so that a bunch of items spawn at designed positions when the character is at a certain distance from the main area part, the thing is, when i hit play to test it out, it either does not run any prints, or runs very few and stops inmediately, i am guessing it’s because of the ammount of things it has to load, since i’m also using runservice.heartbeat for scripts that change transparency and other very simple things.
I also thought it could have been how far the character spawns, but i tested from close to 1000+ stud distance, in both the same thing happened, but again in the smaller scripts with less things to change it worked with no problems.
local cs = game:GetService("CollectionService")
local rs = game:GetService("RunService")
local objs = cs:GetTagged("objspawner")
local zone1 = game.Workspace.Zone1
local plr = game.Players.LocalPlayer
local function spawnparts()
for i, obj in ipairs(objs) do
local list = game.ReplicatedStorage.pumpkin:GetChildren()
local part = list[math.random(1,#list)]
local par = part:Clone()
par.Parent = zone1
par:PivotTo(obj:GetPivot())
end
end
rs.Heartbeat:Connect(function()
local distance = math.round(plr:DistanceFromCharacter(script.Parent.Position))
print("distance: "..distance)
if distance <= 200 then
if #zone1:GetChildren() == 0 then
spawnparts()
print("function ran")
else
end
else
if #zone1:GetChildren() > 0 then
for _, pumpk in zone1:GetChildren() do
pumpk:Destroy()
end
end
end
end)