Game teleporter script

What I want to achieve is for when you touch a part it will teleport you to a random game within a set of IDs. I want it to grab a new ID out every 5 seconds. The issue is that I don’t know how to make it generate a new one every 5 seconds. I tried putting the gameID variable into an infinite loop to have it keep generating game IDs every 5 second but the script gets far too confused about it.

(also feel free to point out any mistakes in my code that are unrelated!!)

local games = {
	"13656338590",
	"5592574257",
	"4800399146",
	"5174502082",
	"11979747919",
	"11601938911"
}
local gameID = table[math.random(#games)]

function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		game:GetService("TeleportService"):Teleport(gameID, player)end end
2 Likes

Well, I’m not sure if this is what you need, but the solution is simple, it’s a matter of using games[math.random(1, #games)] instead of table[math.random(#games)] since it returns a nil value.

local games = {
	13656338590,
	5592574257,
	4800399146,
	5174502082,
	11979747919,
	11601938911
}


function onTouched(hit)
	local gameID = games[math.random(1, #games)]
	
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if player then
		
		game:GetService("TeleportService"):Teleport(gameID, player)
		
	end
end
1 Like

How do I make it change the gameID ever 5 seconds?

I don’t know if you mean there is a cooldown when playing the part, well if it is, this is the solution I can offer you…

local games = {
	13656338590,
	5592574257,
	4800399146,
	5174502082,
	11979747919,
	11601938911
}

local cooldown = true

function onTouched(hit)
	if cooldown then
		cooldown = false
		local gameID = games[math.random(1, #games)]

		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player then
			print("Test")
			game:GetService("TeleportService"):Teleport(gameID, player)
			print(game:GetService("TeleportService"):Teleport(gameID, player))
		end
		
		task.wait(5)
		cooldown = true
	end
end

My apologies for previously marking this as the solution, I am very tired. The script simply isn’t teleporting the player to a random game.

local games = {
	"13656338590",
	"5592574257",
	"4800399146",
	"5174502082",
	"11979747919",
	"11601938911"
}

local gameID

task.spawn(function() -- constantly change in the background
  while wait(5) do
    gameID = games[math.random(#games)] -- you can use 1 argument for math.random()
  end
end)

function onTouched(hit)
  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  if player and gameID then
    game:GetService("TeleportService"):Teleport(gameID, player)
  end 
end

Ok, I guess with these settings it suits your needs…

local games = {
	13656338590,
	5592574257,
	4800399146,
	5174502082,
	11979747919,
	11601938911
}

local cooldown = true
local previous = ''

function onTouched(hit)
	if cooldown then
		cooldown = false
		
		local gameID 
		repeat
			gameID = math.random(1, #games)
		until games[gameID] ~= previous
		
		previous = games[gameID]
				
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player then
			game:GetService("TeleportService"):Teleport(games[gameID], player)
		end
		
		task.wait(5)
		cooldown = true
	end
end

When I touch the part it does nothing. I don’t understand why its not doing anything.

Same case with this one. None of these scripts are even giving me errors.

Ok so the script that somewhat works is this:

local games = {
	"13656338590",
	"5592574257",
	"4800399146",
	"5174502082",
	"11979747919",
	"11601938911"
}

local TeleportService = game:GetService("TeleportService")
local gameID = games[math.random(#games)]

function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		TeleportService:Teleport(gameID, player)
	end
end

script.Parent.Touched:connect(onTouched)

The only issue is that the teleport fails as it says the place is restricted.

Nevermind I fixed it!! Thank you guys for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.