Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 3 months ago

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

Log in to vote
2
Answered by
Nanomatics 1142 Moderation Voter
3 months ago

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.

Ad

Answer this question