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

Number changing to a table when passed into a function?

Asked by
Dog2puppy 168
5 years ago

So I currently have this code here:

if bestMatchId == nil then
            local NewMatch = { Players = {}, MapId = map, updateFunc = function(match, matchPos)
                print("Firing update func with id", matchPos)
                MatchmakingService:FireAllClientsEvent(UPDATE_MATCH_DETAILS_EVENT, matchPos, match.MapId)
                if #match.Players >= 1 then
                    wait(5)
                    local TS = game:GetService("TeleportService")
                    local CodeSuccess
                    local TSCode
                    local Attempt = 0
                    repeat Attempt = Attempt + 1 CodeSuccess, TSCode = pcall(function() TS:ReserveServer(PlaceIds[map]) end) until CodeSuccess == true or Attempt > 5
                    -- Teleport players
                    local TPSuccess, TpMessage
                    local TpAttempt = 0
                    repeat TpAttempt = TpAttempt + 1 TPSuccess, TpMessage = pcall(function() TS:TeleportToPrivateServer(PlaceIds[map], TSCode, match.Players, "Start", {}) end) if TPSuccess == false then warn("TP ERROR:", TpMessage) end until TPSuccess == true or TpAttempt > 5
                    Matches[matchPos] = nil
                end
            end }
            table.insert(NewMatch.Players, player)
            local pos = table.getn(Matches[map]) + 1
            table.insert(Matches[map], pos, NewMatch)
            bestMatchId = pos
            print("Firing update func with pos", pos)
            NewMatch:updateFunc(NewMatch, pos)
        end

It pretty much makes a new dictionary that represents a match, and it has a update function that is called each time the match is updated. When the match is initially created, it's inserted into the main table with all matches, and then prints "firing update func with pos insert pos in main table here", but in the update function itself when I run print("Firing update func with id", matchPos) it prints out that the matchPos part is a table, leading me to believe at some point Roblox is converting my number to a table somehow.

0
What does the code you're using to call the function look like? Try printing 'match' as well. Figure out the contents of both arguments. ScrewDeath 153 — 5y
0
The NewMatch is the table that's being created with the function inside it. The function runs each time I make a change to the table (such as adding a player) with the params match and matchPos. So match is the table with the function being called inside. Dog2puppy 168 — 5y
0
After doing a bit more debugging, I printed out matchPos and match with inspect, and it appears that matchPos went from being a number as intended to being the exact same table as argument #1. Dog2puppy 168 — 5y
1
This is very useful for debugging, I feel like a lot of people don't use it: https://blog.roblox.com/2017/11/lua-debugger-enhancements/ climethestair 1663 — 5y
View all comments (3 more)
0
Oh thanks!! I didn't know that existed! Dog2puppy 168 — 5y
0
I used the debugger you mentioned, but it only showed what I noticed in comment number 3. the matchPos argument that I passed into the function at line 24 was a number as proven by line 23, but at line 3 it's clearly changed to a table somehow, and the debugger shows that its the exact same table as argument #1. So somehow argument #2 was set to equal argument 1. My only idea on how to solve this Dog2puppy 168 — 5y
0
is to put the update function outside of the match table. Dog2puppy 168 — 5y

1 answer

Log in to vote
0
Answered by
Dog2puppy 168
5 years ago
Edited 5 years ago

I solved my issue my putting the update function outside of the table. For some reason, Lua seems to change the argument #2 to be the exact same as #1 in the function.

EDIT: I'm just looking back at this question, and now I see why what happened did. When calling the update function, I was using : so the first argument is itself.

Ad

Answer this question