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

i am trying to make a queue system but it gives me an "unable to cast value to Object"?

Asked by 2 years ago

Here is my code:

local requiredPlayers = 3
local playerQueue = {
    1,
    2
}
local ts = game:GetService("TeleportService")
local numOfPlayers = 1

local function hasPlayer(tab, ply)
    for i, v in pairs(tab) do
        if v == ply then
            return true
        end
    end
    return false
end

local function joinQueue(player)
    if not hasPlayer(playerQueue, player) then
        if numOfPlayers == 0 then
            numOfPlayers = 1
        else
            if numOfPlayers == 1 then
                numOfPlayers = 3
                print("player has joined the queue")
                playerQueue[numOfPlayers] = player
            end
        end
    end
end

local function queueLoop()
    print(1)
    while wait(1) do
        print(2)
        if numOfPlayers == requiredPlayers then
            print("prepare to teleport")
            local code = ts:ReserveServer(11299666436)
            print(3)
            ts:TeleportToPrivateServer(11299666436, code, playerQueue)
            print(4)
            playerQueue = {}
            print(5)
        end
    end
end

spawn(queueLoop)
script.Parent.Touched:Connect(joinQueue)

if any help could be provided it would save me

1 answer

Log in to vote
0
Answered by
Lakodex 711 Moderation Voter
2 years ago
Edited 2 years ago

Can we get a line of error? I will edit my reply when provided.

EDIT:

It's because you're trying to Cast number values, to objects. TeleportToPrivateServer (third arg) requires a list of player OBJECTS, not UserID's. In the script you put 1, and 2 which aren't player objects and are number values.

I would also recommend table.insert(playerQueue,player) instead of using **playerQueue[numOfPlayers] = player ** as this is incredibly insufficient and can cause bugs.

(Sorry for late reply. Been busy.)

Here is an updated code just to help with your troubles. This is also correct. I would also discontinue the use of local functions unless you are creating a function, within a function. It just makes it a bit harder to use it in a proper manner.

local requiredPlayers = 3
local playerQueue = {}
local ts = game:GetService("TeleportService")

function hasPlayer(tab, ply)
    if table.find(tab,ply) then
        return true
    end
    return false
end

function joinQueue(player)
    if not player or not player:IsA("Player") then print("No Player Object. Discontinuing request.") end
    if not hasPlayer(playerQueue, player) then
        table.insert(playerQueue,player)
        queueUpdate()
        print(player.Name.." added to queue.")
    else
        print("Player is already in queue. Discontinuing request.")
    end
end

function leaveQueue(player)
    if not player or not player:IsA("Player") then print("No Player Object. Discontinuing request.") end
    if hasPlayer(playerQueue,player) then
        table.remove(playerQueue,table.find(playerQueue,player))
        queueUpdate()
        print(player.Name.." removed from queue.")
    else
        print("Player does not exist in queue. Discontinuing request.")
    end
end

function queueUpdate()
    if #playerQueue >= requiredPlayers then
        for i=1,10 do --Were going to do this for if anyone decides to join in the queue on accident.
            print(i.." until start.")
            if #playerQueue < requiredPlayers then
                print("Not enough players. Discontinuing request.")
                break
            end
        end
        print("Preparing to teleport from queue.")
        local code = ts:ReserveServer(11299666436)
        ts:TeleportToPrivateServer(11299666436, code, playerQueue)
        playerQueue = {}
    end
end

script.Parent.Touched:Connect(joinQueue)
0
This is what it says: 18:56:21 -- Unable to cast value to object VictoreRoyale667 8 — 2y
0
18:56:21 -- Unable to cast value to object Stack Begin Script 'Workspace.Pad1.Script', line 40 - function queueLoop Stack End VictoreRoyale667 8 — 2y
1
Are you gonna respond VictoreRoyale667 8 — 2y
Ad

Answer this question