Button that makes player teleport to random part

The problem I’m facing is most probably a simple one, I was able to make a script that would teleport me to one specific part, but I’m trying to make a button teleport me to a random part out of 10 parts, and I’m not good with “Math.random” scripts.
Here is the script I’m currently using:

1 Like

If you have a table of all the possible parts to teleport to, you can simply use parts[math.random(#parts)], like so:

local parts = {game.Workspace.Part1, game.Workspace.Part2, game.Workspace.Part3}

script.Parent.MouseButton1Click:Connect(function()
    local target = parts[math.random(#parts)]
    -- Teleport the player to the target part
end)

Instead of manually populating the parts table, you can put a Folder inside of Workspace and put all the target parts there, then you can simply do:

local parts = game.Workspace.TargetParts:GetChildren()

Note: You shouldn’t be teleporting on the client

4 Likes

Create a table of all the random parts that a player could teleport to and randomly scroll through the tables index to get a random part’s position.

1 Like