Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to randomly tp an object to another object?

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

2 comments

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?

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.

1 answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.