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

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

Asked by 5 years ago
Edited 5 years ago

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!

0
game.Workspace.Map.Parent = nil IrishStukov 20 — 5y

2 answers

Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
Thanks mate, worked like a charm ! You're a legend RIBBENTROPP 45 — 5y
0
Np jaschutte 324 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
Thanks for the explanation, I'd upvote if could! What's the downside of using lighting for storage though ? RIBBENTROPP 45 — 5y
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. theking48989987 2147 — 5y
0
Ah I see, thanks for the reply! RIBBENTROPP 45 — 5y

Answer this question