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

Trying to randomly tp an object to another object?

Asked by 5 years ago

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

0
sorry i dont have an answer but just 1 question, why is AvaibleItemSpawns a random number between 1 and ItemSpawns? if there were 10 spawns you could potentially only teleport 1. you say you have to teleport as many trees as there are item spawns. if i read it all wrong forgive me, but it seems to be a flaw? Spiritlotus 151 — 5y
0
also the "table expected, got number" error occurs because its just a number with no significant value. its a random number. you need a table of all your spawn points. Spiritlotus 151 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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!

Ad

Answer this question