Teleport not working

Hi, so recently, I have been trying to create a minigame game. But, I got an error and I don’t know how to fix it. Here’s a screenshot:

Your while true do loop needs a wait() in it.

When the countdown hits 0, it doesn’t teleport me to a random location.

Try setting the CFrame of the HumanoidRootPart to a little above the teleport pad, instead of moving the character

I don’t know how to do that. Sorry but I just started coding more often. Also I watched this video: https://www.youtube.com/watch?v=Kmm2BXZGRgc

It’s ok. Do this:
Ln 13 players[i].Character.HumanoidRootPart.CFrame = workspace.Teleports:GetChildren()[spawnLocation].CFrame + CFrame.new(0, 10, 0)

players[i].Character.HumanoidRootPart.CFrame: Here we are getting the CFrame of the player’s root part. We need this because it can control the entire player’s character.

workspace.Teleports:GetChildren()[spawnLocation].CFrame + CFrame.new(0, 10, 0): This gets the teleport pad’s CFrame, or position, and then adds 10 y units to that, so now the player will go to the pad + 10 units above it.

Update: Don’t need to do this, check my recent reply.

Wait nevermind. I see what you did. Your for loop starts at 10, not 1. Those numbers got switched around.

@Dev_BB1 could set up their for loop using

for i = 10, 0, -1 do

Which would allow the loop to go backwards by defining what you’re moving by, and then change it to display i instead of i-10

for some reason, it teleports me when the number is at 8. And the number switches to 0 after every number.

You need to do -1 before the do part. Else, it won’t go down.

it goes down, but the value goes to 0 after every number. It goes 10,0,9,0… You get the point.

Actually, I fixed the countdown problem. But, it teleports me after every 2 seconds.

Try this:


while true do
	for i = 10, 1, -1 do
		status.Value = ‘Intermission ‘.. tostring(i-1)
		wait(1)
	end
	local players = game.Players:GetChildren()
	for i = 1, #players do 
		if players[I].Character ~= nil then
			local spawnLocation = math.random(1, #workspace.Teleports:GetChildren())
			players[I].Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
		end
	end
end

Sorry I couldn’t help that fast, I’m new to helping.

Teleporting requires changing the HumanoidRootPart’s position.

See below (sample code):

local objToTP = workspace.Part

for i,player in next, game:GetService('Players'):GetPlayers() do
      if player.Character then
           player.Character.HumanoidRootPart.CFrame = objToTP.CFrame*CFrame.new(0,2,0)
      end
end

In short, MoveTo makes the player move by walking somewhere, not teleporting somewhere.

1 Like

Do I copy and paste this into the script? If so, where?

That gave me two errors:

Looks like you want to Teleport all players to the random spawn locations.
Try this

local Players = game.Players:GetPlayers()
for i, v in pairs(Players) do -- For all Players
     if v.Character ~= nil then 
        local Spawn = math.random(1, #workspace.Teleports:GetChildren())
        v.Character:SetPrimaryPartCFrame(Spawn.Position + Vector3.new(0,10,0)) --Higher than the spawn position             
     end
end

You are missing an end after line 17.

2 Likes

I think you misunderstood me. I want my character to teleport to two specific parts when the intermission hits 0.

Hello,

The error

Script timeout: exhausted allowed execution time

Means that your script is running extremely fast that it’s exhausted because it’s gone past the allowed execution time for scripts, to fix this you could simply just add a wait().

I also see so many mistakes within your script, firstly you shouldn’t use while true do because it has negative effects and could cause issues within your game instead you could use something like the .Changed or even the :GetPropertyChangedSignal() events I will include an example of a better method to teleport the player also I highly recommend you to not handle the PlayerGui UIs from the Server instead handle them from the Client.

Secondly your indentation is horrible and can be easily fixed by going here

-- Don't forget to double check on the path of the thing that you want the player to be teleported to

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local HRP = LocalPlayer.Character.HumanoidRootPart
HRP.CFrame = game.Workspace.Teleports:GetChildren()[spawnLocation].CFrame