How to randomly pick someone and teleport them to a block

Hey all, don’t know if this has been made before (couldn’t find it, if there is), but I seek your help! Could anyone guide me in the right direction? I would like someone to randomly be selected and teleported on stage. For example, rap battle simulator. Could anyone help me out? Thanks!

5 Likes

Hello, you can get a table of the players in your place using game.Players:GetPlayers(). Then use math.random() to generate a random number, and select the player who corresponds to that number in the table.

2 Likes

Try this:

local function selectPlayer()
    players = game.Players:GetPlayers()
    selected = players[math.random(1,#players)]
    return selected
end

This should return a random player which you can then teleport by:

randomPlayer = selectPlayer()
if randomPlayer and randomPlayer.Character then
    randomPlayer.Character:MoveTo(part.Position) -- part is the part you want to tp them to
-- Or teleport them using CFrame
    randomPlayer.Character.HumanoidRootPart.CFrame = part.CFrame
end

I recommend using HumanoidRootPart if you teleport via CFrame because both R6 and R15 have these

14 Likes
local function TeleportRandomToPart(part)
    local plyrs = game:GetService("Players"):GetPlayers()
    local char = plyrs[math.random(1,#plyrs)].Character
    if char and char.HumanoidRootPart then
        char.HumanoidRootPart.CFrame = part.CFrame + Vector3.new(0,2.5,0)
    end
end
4 Likes