0

# How to make a random player picker and teleport them to a block?

Edited 22 days ago

Hello. I have a Scripting question. So, I am trying to make a game where basically a player gets teleported on a stage for 45 seconds, and then after that 45 seconds, that player gets teleported back to spawn. So basically, a random player gets teleported to a part for a specific amount of time (the stage), and after that amount of seconds, they get teleported back to another part (which is the spawn) Help would be appreciated. If you don't understand but want to help, please ask so.

So far, this is what I have put to select a random player:

wait(5)
local num = math.random(1, #game.Players:GetChildren())
print(game.Players:GetChildren()[num])
local player


Then, I have tried making it so it teleports the player:

wait(5)
local num = math.random(1, #game.Players:GetChildren())
print(game.Players:GetChildren()[num])
local player
player.CFrame = CFrame.new(11.667, 23.652, 95.261) -- position
player = nil


The problem is that it doesn't teleport the player and an error says "attempt to index nil with 'CFrame' ". Please help me fix it so it actually teleports the player and make it so it after 45 seconds it teleports that player back to another block (the spawn)

1
Edited 22 days ago

As I've mentioned in my comment in the other answer, the other answer is trying to index the child of a print() call, which obviously does not exist. Also, you're trying to set the CFrame property of a Player Instance.

Instead, it should be like this:

task.wait(5) -- task.wait is the "updated" version of wait and is more precise
local num = math.random(1, #game.Players:GetChildren())
local player = game.Players:GetChildren()[num]

print(player.Name)

local hrp = player.Character.HumanoidRootPart
hrp.CFrame = CFrame.new(Vector3.new(11.667, 23.652, 95.261)) -- Teleport to where you wanted
hrp.CFrame = CFrame.new(Vector3.new(0,0,0)) -- Teleport back to spawn

0
oh i understand now! Cubispaghettis 1 — 22d
0
Edited 22 days ago

You gotta set the player to the random player

wait(5)
local num = math.random(1, #game.Players:GetChildren())
print(game.Players:GetChildren()[num]).Character.HumanoidRootPart
local player = game.Players:GetChildren()[num]
player.CFrame = CFrame.new(Vector3.new(11.667, 23.652, 95.261)) -- position
player = nil
-- teleporting to spawn
wait(45)
player.CFrame = CFrame.new(Vector3.new(0,0,0)) -- or whatever the position of the spawn is. You can also get the position of the spawn part to do this.

0
Oh! Thanks @JustinWe12 but do you know how to make the teleport back to spawn after the 45 seconds? Cubispaghettis 1 — 22d
0
Also, it doesn't work it says "CFrame is not a valid member of Player "Players.CubiSpaghettis" Cubispaghettis 1 — 22d
0
Oh mb, should work now JustinWe12 723 — 22d
0
It still has an error on line 2 that says "Incomplete statement: expected assignment or a function call" Cubispaghettis 1 — 22d
0
Do i have to something? Cubispaghettis 1 — 22d
0
0
It's likely because you're trying to index a child of a print statement. Also you're trying to set the CFrame of a Player, you have to set the CFrame of his HumanoidRootPart instead. DindinYT37 212 — 22d
0
so what would i do? Cubispaghettis 1 — 22d
0
Edited 16 days ago

It's because you didn't set the player variable, just as what @JustinWe12 said.

Let's say you have Part named StagePart in Workspace and a SpawnLocation where all player spawns.

To teleport the Player to StagePart, you don't need to set Player.CFrame because the Player Instance doesn't have a CFrame. Instead, get the Player's character model using Player.Character and use its CFrame using Character:GetPivot().

To teleport the Player back to spawn, we simply do Player:LoadCharacter() because it respawns the player. When a Player respawns it will automatically teleport them to a SpawnLocation.

Also we should make a BindableEvent that will detect if Player left or died.

The script should look like this:

local Players = game:GetService("Player")
local StagePart = workspace.StagePart
local Spawn = workspace.SpawnLocation

local bindableEvent = Instance.new("BindableEvent")

function tpRandomToStage()

local allPlayers = Players:GetPlayers() -- basically :GetChildren() but only choose Players
local randomNumber = Random.new(os.time()):NextInteger(1, #allPlayers)
local randomPlayer = allPlayers[randomNumber]
local randomCharacter = randomPlayer.Character or randomPlayer.CharacterAdded:Wait()
local randomHumanoid = randomCharacter:FindFirstChildWhichIsA("Humanoid")

randomHumanoid.Died:Connect(function() -- when the player died
bindableEvent:Fire()
end)

randomPlayer.CharacterAdded:Connect(function() -- when the player respawns
bindableEvent:Fire()
end)

Players.PlayerRemoving:Connect(function(player) -- when a player leaves
if player == randomPlayer then -- if the player leaving is the same as the player on the stage
bindableEvent:Fire()
end
end)

bindableEvent.Event:Connect(function()
return -- stops/breaks the whole function
end)

-- PivotTo and GetPivot are both CFrame
randomCharacter:PivotTo(StagePart:GetPivot()) -- teleports the player

You can use while true do loop T3_MasterGamer 1701 — 22d