Make teleporter script be randomized

So I have a teleporter script thats attatched to a gui button. Once clicked it will teleport the player the a part labeled spawn. What I want it to do is to teleport the player to one of four spawn boxes. How would I go about doing that?

Heres the script:

player = game.Players.LocalPlayer
button = script.Parent
local debounce = false

function teleport()
	if not debounce then
		debounce = true
		local Torso = player.Character.Torso
		Torso.CFrame = game.Workspace.Spawn.CFrame
	end
end

button.MouseButton1Click:Connect(teleport)
while true do wait()
	debounce = false
	wait(1)
end

You can create a folder which holds your spawns. When clicking the teleport button, pick a random spawn from the spawns folder children.

local player = game.Players.LocalPlayer
local button = script.Parent
local debounce = false

local Spawns = path.to.spawns

function teleport()
	if not debounce then
		debounce = true
		local Torso = player.Character.Torso
		Torso.CFrame = Spawns:GetChildren()[math.random(1, #Spawns:GetChildren())].CFrame
	end
end

button.MouseButton1Click:Connect(teleport)
while true do wait()
	debounce = false
	wait(1)
end

The code to choose a random child of some object is:

local Object = game.Workspace
local RandomChild = Object:GetChildren()[math.random(1, #Object:GetChildren())]
1 Like