Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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!

0
connect is deprecated, use Connect User#19524 175 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

0
Thanks for the clarifications. GamerOkami 71 — 6y
Ad

Answer this question