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

Can you help me fix the error: attempt to call local "xpLvlTable" (a table value)? [SOLVED]

Asked by 5 years ago
Edited 5 years ago

I get the error attempt to call local "xpLvlTable" (a table value) and I have a metatable inside the table that is activated with __call when the table is called

'Script'

-- Variables
local xp_lvlDataStore = game:GetService('DataStoreService'):GetDataStore('xp/lvlStore')
local repStorage = game:GetService('ReplicatedStorage')
local xpChanger = repStorage:WaitForChild('Xp_LvlChanged')
local xpToAdd = 5
local part = script.Parent
local maxLvl = 350

-- Events
pcall(part.ClickDetector.MouseClick:Connect(function(plr)
    local key = 'Player-'..plr.UserId
    local xpLvlTable = xp_lvlDataStore:GetAsync(key)
    -- Fire to player
    xpChanger:FireClient(plr,xpToAdd)
    -- Sends added xp then saves
    xpLvlTable(xpToAdd)
end))

It errors on line 16

'Table'

-- sets the xp to 0 if plr is new
            local xpLvlTable = {0,0}
            local LvlHandler = {
                -- Fired when xpLvlTable is called
                __call = function(xpLvlTable,xpToAdd)
                    -- Gets current lvl and xp
                    local curLvl = xpLvlTable[2]
                    local newXp = xpLvlTable[1] + xpToAdd
                    -- Checks if you lvled up
                    if curLvl ==  0 and newXp >= 10 then
                        print('FIRST LVL UP')
                        local newXp = newXp - 10
                        -- Checks if can lvl up again
                        for i = curLvl,350 do
                            if i == 350 then
                                table.remove(xpLvlTable,1)
                                table.insert(xpLvlTable,1,0)
                                table.remove(xpLvlTable,2)
                                table.insert(xpLvlTable,2,350)
                            elseif newXp - (10*i) < 0 then
                                -- Sets xp + lvl in the table then saves
                                table.remove(xpLvlTable,1)
                                table.insert(xpLvlTable,1,newXp)
                                table.remove(xpLvlTable,2)
                                table.insert(xpLvlTable,2,i)
                                xp_lvl_DataStore:SetAsync(key,xpLvlTable)
                                break
                            else
                                newXp = newXp - (10*i)
                            end
                        end
                    elseif curLvl == 350 then
                        print('MAX LVL')
                        -- Sets xp + lvl in xpLvlTable then saves
                        table.remove(xpLvlTable,1)
                        table.insert(xpLvlTable,1,0)
                        xp_lvl_DataStore:SetAsync(key,xpLvlTable)
                    elseif newXp >= 10*curLvl then
                        print('LVL UP')
                        -- Gets current lvl and xp
                        local curLvl = xpLvlTable[2]
                        local newXp = xpLvlTable[1] + xpToAdd
                        -- Checks if you lvled up
                        local newXp = newXp - 10*curLvl
                        -- Checks if can lvl up again
                        for i = curLvl,350 do
                            if i == 350 then
                                table.remove(xpLvlTable,1)
                                table.insert(xpLvlTable,1,0)
                                table.remove(xpLvlTable,2)
                                table.insert(xpLvlTable,2,350)
                            elseif newXp - (10*i) < 0 then
                                -- Sets xp + lvl in the xpLvlTable then saves
                                table.remove(xpLvlTable,1)
                                table.insert(xpLvlTable,1,newXp)
                                table.remove(xpLvlTable,2)
                                table.insert(xpLvlTable,2,i)
                                xp_lvl_DataStore:SetAsync(key,xpLvlTable)
                                break
                            else
                                newXp = newXp - (10*i)
                            end
                        end
                    else
                        print('NO LVL')
                        -- Sets xp then saves
                        table.remove(xpLvlTable,1)
                        table.insert(xpLvlTable,1,newXp)
                        xp_lvl_DataStore:SetAsync(key,xpLvlTable)
                    end
                end
            }
            setmetatable(xpLvlTable,LvlHandler)
            xp_lvl_DataStore:SetAsync(key,(xpLvlTable))
            return xp_lvl_DataStore:GetAsync(key)
        end
    end
0
Is this two seperate scripts? If so when you assign xpLvlTable to xp_lvlDataStore:GetAsync(key) on line 12, GetAsync(key) returns Variant. A Variant can refer to multiple types of data like tables, strings, numbers, etc... So you don't know what exactly you are returning there so that's wrong. Also you are in a seperate script so even if you do return a table you never setmetatable in that script. Impacthills 223 — 5y
0
You should use a module script so you can return a table with a metatable already set. Impacthills 223 — 5y
0
So returns a table and does the actions and sets the other table and saves it ty. RichCookieGamer 7 — 5y

Answer this question