Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why wont my 'ReserveServer' Teleport script work?

Hello i am making a horror game with a roblox friend and making some good progress. However, i have encountered a problem with the teleportation side of things. I am wanting to use the ReserveServer property to make it so when players are in, noone else can get into that server. As of so far i have this code:

local plrQuantity = script.Parent.Values.plrQuantity
local Players = script.Parent.Players
local TeleportService = game:GetService("TeleportService")
local Place = 2060347621 -- Main Game


local MinimumPlayers = 1


while true do
	wait()
	if plrQuantity.Value >=MinimumPlayers then
		print("Enough Player to start.")
		wait(5)
		if plrQuantity.Value >=MinimumPlayers then
			local code = TeleportService:ReserveServer(Place)
			print("Teleporting Players")
			
			for name, child in pairs(Players:GetChildren()) do
				local temp = game.Players:GetUserIdFromNameAsync(child.Name)
				local plr = game.Players:GetPlayerByUserId(temp)
				--local plr = game.Players:GetPlayerFromCharacter(child)
				
				TeleportService:TeleportToPrivateServer(Place,code,plr)
				plrQuantity.Value = 0				
				
				for name, child in pairs(Players:GetChildren()) do
					child:Destroy()
				end		
			end
		end
	end
end

But i get errors in-game saying: "Unable to cast value to Objects" -- line 24 If anyone knows how to fix this I'd very much appreciate help. Thanks. BTW: "local Players = script.Parent.Players" is not the game.Players Service its a folder containing String values of usernames.

1 answer

✓ Accepted answer

TeleportToPrivateServer needs an array of players to teleport. You are passing one player which is causing your error.

You can also usee the function GetPlayers to return a array of players.

Lastly you should use events in this case not a loop as plrQuantity has a changed event.

Example:-

local plrQuantity = script.Parent.Values.plrQuantity
local plrServ = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local Place = 2060347621
local MinimumPlayers = 1
local TeleportWait = false

plrQuantity.Changed:Connect(function(newVal)
	if TeleportWait then return end -- to help stop multiple calls to teleport
	
	-- count less than max
	if newVal < MinimumPlayers then return end
	TeleportWait = true
	wait(5)
	if plrQuantity.Value < MinimumPlayers then 
		-- reset
		TeleportWait = false 
		return 
	end
	
	TeleportService:TeleportToPrivateServer(
		Place, 
		TeleportService:ReserveServer(Place),
		plrServ:GetPlayers()
	)
	TeleportWait = false -- reset
	plrQuantity.Value = 0 
end)

I hope this helps.

2 comments

0

Also, wont this tp all players? Because i'd only want it to TP the players who have entered the queue.

0

in this case you would need to make your own array of players that are in the queue and exclude the other players.

This discussion is preserved as it was. New answers, comments, and votes are closed.