Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to randomly pick an object from lighting, place in workspace, then remove after a time?

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.

2 comments

0

What error is it giving you?

0

I actually don't seem to be getting any errors, the model just doesn't want to remove.

2 answers

✓ Accepted answer
  1. Why do you use Lighting for storage? You should have used ServerStorage!
  2. :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!

5 comments

0

That didn't exactly help.. Everything worked accept for the part when it Destroys. When the time is up, the model doesn't destroy, it just stays and continues to work

0

All you have to do is move the kler definition to inside of the while loop so it gets a new random one each time.

0

That didn't help.. The model stays in the workspace and doesn't remove.

0

The answer is updated because I found out why!

0

It worked, 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.

1 comment

0

lol yes! This also works!

This discussion is preserved as it was. New answers, comments, and votes are closed.