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!
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.
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