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

Script output says "Unable to cast value to object" How can I fix?

I am a bit new to Lua and I have this script:

local gameID = 2034957882
local TeleportService = game:GetService("TeleportService")
local serverCode = TeleportService:ReserveServer(gameID)

 
function onTouch(hit)
    local playersInSeats = game.Players:GetPlayerFromCharacter(hit.Parent)
    if playersInSeats then
        TeleportService:TeleportToPrivateServer(gameID,serverCode,playersInSeats)
    end
end
 
script.Parent.Touched:connect(onTouch)

Pretty much, It is supposed to make a new server and send all players touching it to the server (I think) and it says that on 9 line it couldn't cast value to object. How can I fix this? Thanks!

1 answer

The problem is with the 3rd argument of TeleportToPrivateServer, it is expecting an array of player objects, you are sending an object (the player).

So to solve that, you can make an array, insert the player into that array, and then send the array of the players as the 3rd argument.

So it should look something like this:

local gameID = 2034957882
local TeleportService = game:GetService("TeleportService")
local serverCode = TeleportService:ReserveServer(gameID)


function onTouch(hit)
    local arrayOfPlayers = {} 
    local playersInSeats = game.Players:GetPlayerFromCharacter(hit.Parent)
    table.insert(arrayOfPlayers, playerInSeats) -- add the player into the array
    if playersInSeats then
        TeleportService:TeleportToPrivateServer(gameID,serverCode, arrayOfPlayers) -- send array
    end
end

script.Parent.Touched:connect(onTouch)

Hope I helped, if you have any questions please let me know.

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