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)
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 task.wait(45) -- Wait 45 Seconds hrp.CFrame = CFrame.new(Vector3.new(0,0,0)) -- Teleport back to spawn
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.
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() task.wait(5) -- task.wait() is slightly faster than wait() 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 task.wait(45) randomPlayer:LoadCharacter() -- respawns the player and teleports back to spawn end tpRandomToStage()