Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Mayk728 855 Moderation Voter
7 years ago
Edited 7 years ago

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.
0
What error is it giving you? evilpuffle 0 — 7y
0
I actually don't seem to be getting any errors, the model just doesn't want to remove. Mayk728 855 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago
  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!

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 Mayk728 855 — 7y
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. Pyrondon 2089 — 7y
0
That didn't help.. The model stays in the workspace and doesn't remove. Mayk728 855 — 7y
0
The answer is updated because I found out why! starlebVerse 685 — 7y
0
It worked, Thanks! Mayk728 855 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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.
0
lol yes! This also works! starlebVerse 685 — 7y

Answer this question