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

How do I remove an instance of a model (without hard-coding it) when I clone it?

I've got a small script which randomly selects a map from a selection of 2 models, I'm having trouble removing them once they are in -

local num = math.random(1,2)
local map = game.Lighting:WaitForChild("Map" ..num)
map:clone().Parent = game.Workspace
game.Workspace.map:Remove()

The last line is where I am having an error, as 'map is not an instance of Workspace' which I can only assume is because it is acting as a pointer here rather than the actual instance of the model, how do I remove the model named, for example Map1 or Map2? Which are randomly selected, so I can't hard-code their names in.

Much appreciated any answers!

1 comment

0

game.Workspace.Map.Parent = nil

2 answers

✓ Accepted answer

This can work:

local num = math.random(1,2)
local map = game.Lighting:WaitForChild("Map" ..num)
map:clone().Parent = game.Workspace
game.Workspace["Map"..num]:Destroy() --:Remove() is deprecated.

But I prefer and recommand using this:

local num = math.random(1,2)
local map = game.Lighting:WaitForChild("Map" ..num)
local wMap = map:clone()
wMap.Parent = game.Workspace
wMap:Destroy() --:Remove() is deprecated.

The second code block is better because if you have another object in workspace named 'Map1' for example, it will destroy that instaid of the actual map.

2 comments

0

Thanks mate, worked like a charm ! You're a legend

0

Np

the problem here is that this isn't using the variable you set up called map, but it is just searching for an instance or property called "map" in workspace, which probably doesn't exist. To fix this you can use either the WaitForChild or the FindFirstChild functions, or you could just index it.

For example:

local num = math.random(1,2)
local map = game.Lighting:WaitForChild("Map" ..num)--don't use lighting as storage
map:clone().Parent = workspace
game.Workspace[map.Name]:Destroy()--use :Destroy(), :Remove() is deprecated

or

local num = math.random(1,2)
local map = game.ServerStorage:WaitForChild("Map" ..num)--don't use lighting as storage
map:clone().Parent = workspace
game.Workspace:FindFirstChild(map.Name):Destroy()--use :Destroy(), :Remove() is deprecated

Also, you can assign a variable to the map and destroy that if need be

local num = math.random(1,2)
local map = game.ServerStorage:WaitForChild("Map" ..num)--don't use lighting as storage
local CloneMap = map:clone()
CloneMap.Parent = workspace
CloneMap:Destroy()--use :Destroy(), :Remove() is deprecated

3 comments

0

Thanks for the explanation, I'd upvote if could! What's the downside of using lighting for storage though ?

0

Well, it is simply more understandable to place things into server storage. The reason using lighting as a storage is frowned upon is probably a mix of convention and organization as some things have to be in lighting such as color correction, the bloom effect, the blur effect, and etc.

0

Ah I see, thanks for the reply!

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