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

Error: "Unable to cast value to Object" involving ReserveServer()?

I have a multi-game place with the starter place as a hub where you can teleport to all the other places from. For a while, I've been using this to get from place to place with success:

game:GetService("TeleportService"):Teleport(placeID, player)

Then I found out that my friends in the U.K never get into the same servers as my other friends here in the United States, even though no server capacity limits have been reached. So I've decided to use the ReserveServer() function of the TeleportService to guarantee everyone gets into the same server.

I eventually coded this into my server script and testing it to see that I get the error "Unable to cast value to Object" on line 11 (eleven) when someone wants to teleport to the place called Holo. Here's the code (which is a normal script in ServerScriptService):

wait(1)
local TeleportPlayer = game:GetService("ReplicatedStorage"):WaitForChild("TeleportPlayer")--event that's called when a player wants to teleport to a place
local TeleportService = game:GetService("TeleportService")--teleport service

local HoloReserveCode = TeleportService:ReserveServer(2164207568)--holo code
local FortReserveCode = TeleportService:ReserveServer(2164380789)--fort code
local BattleReserveCode = TeleportService:ReserveServer(2164277365)--battlegrounds code

TeleportPlayer.OnServerEvent:connect(function(player, placeID)
	if placeID == 2164207568 then --holo
		TeleportService:TeleportToPrivateServer(placeID, HoloReserveCode, player)
	elseif placeID == 2164380789 then --fort
		TeleportService:TeleportToPrivateServer(placeID, FortReserveCode, player)
	elseif placeID == 2164277365 then--battlegrounds
		TeleportService:TeleportToPrivateServer(placeID, BattleReserveCode, player)
	end
end)

I can use an explanation for why there's an error and a potential fix. Please comment below with more information you may need, and thanks for reading!

1 comment

0

connect is deprecated, use Connect

1 answer

✓ Accepted answer

###This is because the third argument to TeleportToPrivateServer is an array, which consists of the Player objects to teleport.

wait(1)
local TeleportPlayer = game:GetService("ReplicatedStorage"):WaitForChild("TeleportPlayer")--event that's called when a player wants to teleport to a place
local TeleportService = game:GetService("TeleportService")--teleport service

local HoloReserveCode = TeleportService:ReserveServer(2164207568)--holo code
local FortReserveCode = TeleportService:ReserveServer(2164380789)--fort code
local BattleReserveCode = TeleportService:ReserveServer(2164277365)--battlegrounds code

TeleportPlayer.OnServerEvent:Connect(function(player, placeID)
    if placeID == 2164207568 then --holo
        TeleportService:TeleportToPrivateServer(placeID, HoloReserveCode, {player})
    elseif placeID == 2164380789 then --fort
        TeleportService:TeleportToPrivateServer(placeID, FortReserveCode, {player})
    elseif placeID == 2164277365 then--battlegrounds
        TeleportService:TeleportToPrivateServer(placeID, BattleReserveCode, {player})
    end
end)

###Simply wrap player in curly braces to make it an array.

###On a side note RBXScriptSignal:connect() is deprecated and should not be used in new work, switch to RBXScriptSignal:Connect() instead.

1 comment

0

Thanks for the clarifications.

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