I want it so the script grabs a random NPC from lighting then place it into workspace for a certain amount of time, then remove it and repeat. I got the part where it randomly picks and adds it to workspace but I don't know how to make it remove without having errors.
--Get NPCs from Lighting local Lighti = game.Lighting:FindFirstChild("NPCs"):GetChildren() local kler = Lighti[math.random(1, #Lighti)] while true do wait(0.05) kler:Clone().Parent = game.Workspace wait(100) --Just a random time I picked kler:Remove() end --Let's say inside the NPCs model there is "NPC1" and "NPC2", I don't know how to get one to be removed after it's time for it to be removed.
ServerStorage
!:Remove()
is deprecated! Use :Destroy()
instead!!Here is the code for you:
--Put all the NPCs into ServerStorage!! --Get NPCs from ServerStorage local npcs = game.ServerStorage:WaitForChild("NPCs"):GetChildren() -- Waits for the child! while true do local chosen = npcs[math.random(1, #npcs)] local kler = chosen:Clone() -- Make the reference kler to the clone of the chosen model. wait(0.05) kler.Parent = game.Workspace wait(100) --Just a random time I picked kler:Destroy() end --Let's say inside the NPCs model there is "NPC1" and "NPC2", I don't know how to get one to be removed after it's time for it to be removed.
UPDATE! So, I found out why the model does not destroy. I am supposed to reference the clone, not the chosen model from ServerStorage
itself!
Any questions? Leave them in the comments below! Thanks!
You should consider using the Debris
service. Like this:
--Put all the NPCs into ServerStorage!! --Get NPCs from ServerStorage local npcs = game.ServerStorage:WaitForChild("NPCs"):GetChildren() -- Waits for the child! local Debris = game:GetService("Debris") while true do local chosen = npcs[math.random(1, #npcs)] local kler = chosen:Clone() -- Make the reference kler to the clone of the chosen model. wait(0.05) kler.Parent = game.Workspace Debris:AddItem(kler, 100) --// Object on the left, time it'll destroy itself is on the right --// Anything under this code will continue because essentially the script isn't technically waiting. end --Let's say inside the NPCs model there is "NPC1" and "NPC2", I don't know how to get one to be removed after it's time for it to be removed.