Im having major issues with my dealer/npc spawning at the very start of the game, when it should spawn after the designated 600 seconds.
A hint i found was that only some of the code seems to be running because it spawns the dealer/npc, but does not display a notification when it would respawn normally.
Any solutions would be great, thank you.
local function spawnDealer()
if IsBrickSpawned.Value then
print("A dealer is already spawned.")
return -- Exit if a dealer is already spawned
end
local randomLocation = spawnLocations[math.random(1, #spawnLocations)]
print("Selected spawn location:", randomLocation.Name)
local dealerClone = DealerNPC:Clone()
dealerClone.Parent = Workspace
if dealerClone.PrimaryPart then
-- Set the position using CFrame
dealerClone:SetPrimaryPartCFrame(randomLocation.CFrame)
-- print("Dealer spawned at:", randomLocation.Position)
--else
-- warn("DealerNPC model does not have a PrimaryPart set.")
end
Debris:AddItem(dealerClone, 300) -
dealerClone.AncestryChanged:Connect(function(_, parent)
if parent == nil then
IsBrickSpawned.Value = false
print("Dealer despawned, IsBrickSpawned reset.")
end
end)
for _, player in ipairs(game.Players:GetPlayers()) do
if not db[player] then
db[player] = true
notificationRE:FireClient(player, notificationData)
-- remove player from debounce after 0.5 secs
task.wait(0.5)
db[player] = nil
end
end
end
while true do
local dealerExists = Workspace:FindFirstChild("BrickManV1") ~= nil
local isBrickSpawned = IsBrickSpawned.Value
print("Dealer exists in Workspace:", dealerExists)
print("IsBrickSpawned.Value:", isBrickSpawned)
if not dealerExists and not isBrickSpawned then
print("Attempting to spawn dealer.")
spawnDealer()
else
print("No need to spawn dealer. Conditions not met.")
end
wait(500) -- Wait 10 minutes
end
while task.wait(500) do
--Alternatively, the wait function can be placed here instead.
local dealerExists = Workspace:FindFirstChild("BrickManV1") ~= nil
local isBrickSpawned = IsBrickSpawned.Value
print("Dealer exists in Workspace:", dealerExists)
print("IsBrickSpawned.Value:", isBrickSpawned)
if not dealerExists and not isBrickSpawned then
print("Attempting to spawn dealer.")
spawnDealer()
else
print("No need to spawn dealer. Conditions not met.")
end
end
All I did was move the wait function to the beginning of the loop, so that the loop doesn’t get ran when the script starts until some time has passed.
Firstly, wait is deprecated. Use task.wait() instead.
Secondly, the reason it starts in the beginning of the game is because the yielding happens after the code runs the first time.
So, change it to this:
while true do
task.wait(600)
local dealerExists = Workspace:FindFirstChild("BrickManV1") ~= nil
local isBrickSpawned = IsBrickSpawned.Value
print("Dealer exists in Workspace:", dealerExists)
print("IsBrickSpawned.Value:", isBrickSpawned)
if not dealerExists and not isBrickSpawned then
print("Attempting to spawn dealer.")
spawnDealer()
else
print("No need to spawn dealer. Conditions not met.")
end
end
By the way something to mention is that this code could be pretty bad performance wise for the server.
This right here would be a memory leak.
This function creates a new event connected to a function everytime the loop happens, granted the yield is quite long so the memory leak could take a while before it starts growing to be an issue. But this would be a server memory leak, servers can last up to days or weeks before shutting down. Meaning it’ll feel like the performance issue is “everywhere” for most players.
Memory leaks on the server can cause all players to crash.
Nitpick: You didn’t have to figure out how many seconds are in 10 minutes in your head, you could’ve simply wrote it like this:
task.wait(10 * 60) -- Would sum up to 600 seconds which equals 10 minutes
Which is also more memorable to know the time, rather than leaving a comment which also works but you know.
By the way, your original 500 seconds was not actually 10 minutes. 10 minutes = 600 seconds, not 500.
Probably use task.wait() as its more effective. The order of your while true do function checks to see if the npc exists, and then spawns it based off the fact there is nothing there before waiting the 500 seconds, 2 possible solutions to this issue would be
task.wait(500) -- Wait 10 minutes
local dealerExists = Workspace:FindFirstChild("BrickManV1") ~= nil
local isBrickSpawned = IsBrickSpawned.Value
print("Dealer exists in Workspace:", dealerExists)
print("IsBrickSpawned.Value:", isBrickSpawned)
if not dealerExists and not isBrickSpawned then
print("Attempting to spawn dealer.")
spawnDealer()
else
print("No need to spawn dealer. Conditions not met.")
end
end
or
while task.wait(500) do
local dealerExists = Workspace:FindFirstChild("BrickManV1") ~= nil
local isBrickSpawned = IsBrickSpawned.Value
print("Dealer exists in Workspace:", dealerExists)
print("IsBrickSpawned.Value:", isBrickSpawned)
if not dealerExists and not isBrickSpawned then
print("Attempting to spawn dealer.")
spawnDealer()
else
print("No need to spawn dealer. Conditions not met.")
end
end
I’m not sure, but it’s definitely a memory leak.
Do you want this to happen every 10 minutes? (in this case you will need to use your loop)
Or do you just want it to happen when a certain condition is met? In that case you can use attributes and use an attributechangedsignal event instead which would be better for performance aswell.