I've been working on a script, it's to teleport a tree(I'm going to add rocks as well) to a part called "ItemSpawns." It confuses me a bit because, one, I have to teleport as many trees as there are ItemSpawns left. And, two, teleporting two parts. (The trees need the 2nd part to be chopped down)
Here is the script-
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Tree = ReplicatedStorage:WaitForChild("Tree").Trunk local TerrainSpawnFolder = workspace:WaitForChild("TerrainSpawns") local ItemSpawns = TerrainSpawnFolder:GetChildren() local AvaiableItemSpawns = math.random(1,#ItemSpawns) --Spawns Trees-- local ClonedTree = Tree:Clone() ClonedTree.Parent = workspace for i, ClonedTree in pairs(AvaiableItemSpawns) do if ClonedTree then --spawn tree ClonedTree.CFrame = AvaiableItemSpawns.CFrame table.remove(AvaiableItemSpawns, i) end if not Tree then -- Don't spawn the tree end end
It's giving me errors on line 15 like " ServerScriptService.GameManager.PlanetTerrain.TerrainSpawner:11: bad argument #1 to 'pairs' (table expected, got number)"
I understand why it's saying this, but, this is the only way I can think of doing this.
If you can help then, Thx
Make a primary part and set the part in the Model property. With that, you can use :SetPrimaryPartCFrame().
It's not a good idea to teleport all parts to one position, since all parts will be in the same position and not the form of a tree.
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Tree = ReplicatedStorage:WaitForChild("Tree").Trunk local TerrainSpawnFolder = workspace:WaitForChild("TerrainSpawns") local ItemSpawns = TerrainSpawnFolder:GetChildren() local AvaliableItemSpawns = math.random(1,#ItemSpawns) local ClonedTree = Tree:Clone() ClonedTree.Parent = workspace if ClonedTree and ClonedTree.PrimaryPart then ClonedTree:SetPrimaryPartCFrame(AvaliableItemSpawns.CFrame) -- table.remove(AvaliableItemSpawns, ClonedTree.PrimaryPart) | added it if you wanted to uncomment it. end
If it works for you, please mark this as answered, else you can tell me what's wrong with the script and the error you're getting.
Thanks!