Hi, what I'm trying to do here is spawn an object named "Foil" to workspace. The "Foil" is in a "Model" named Loot. The script is trying to identify if there is a object named "Foil" in workspace and if there is, then the script won't work. If there is no Foil in workspace, the "Foil" will spawn into the workspace, and move to a part's position.
Material = game.Lighting.Loot:getChildren() Foil = game.workspace:findFirstChild("Foil") function Spawning() for i = 1, #Material do if Material[i] and Material[i]:findFirstChild("Foil") then if Foil.Parent ~= game.workspace then local Materials = Material[i]["Foil"]:Clone() Materials.Parent = workspace local Part = game.Workspace.Part if Part ~= nil then Materials:MoveTo(Part.Position) end elseif Foil.Parent == workspace then local p = nil return p end end end end while true do wait(1) print("Spawning") Spawning() end
If you're wanting to clone the object Foil
into Workspace if there isn't one, then you do not need all that much. All you basically need is a check to see if it's in Workspace, and if not, clone.
local Loot = game.Lighting.Loot:GetChildren() local Name = "Foil" while wait() do if not game.Workspace:findFirstChild(Name) then for i = 1, #Loot do if Loot[i] and Loot[i].Name == Name then local Add = Loot[i]:Clone() Add.Parent = game.Workspace if game.Workspace.Part then Add:MoveTo(Part.Position) end end end end end
if not game.Workspace:findFirstChild(Name)
Checks if there is no Foil in Workspace..
-
if Loot[i] and Loot[i].Name == Name then
Checks if there is a Child of Loot and if it's name is Foil.
-
if game.Workspace.Part then
Checks if there is a part named Part in Workspace.
I hope this clears things up.