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

Can Metatables be used with IntValues, BoolValues, etc?

Asked by 3 years ago
Edited 3 years ago

I'm trying to ensure that no player data in leaderstats is nil by using metatables. But I'm not sure if metatables can be used with Folders and IntValues.

Here's the code I wrote

    local leaderstatsMeta = {}

    leaderstatsMeta.__index = {
        function()
            print("Some data in leaderstats is nil!")
            coins.Value = 1
            multiplier.Value = 1
            rebirths.Value = 0
            steps.Value = 1
        end,
    }

    -- GetChildren returns this but I'm not sure there's another way of doing this or if it works
    --[[
     {
                    [1] = Coins,
                    [2] = Multiplier,
                    [3] = Rebirths,
                    [4] = Steps
    }
    ]]--

    setmetatable(leaderstats:GetChildren(), leaderstatsMeta)

Should I change GetChildren to something else? Is this even possible with metatables? (Sorry if my English is bad it's not my native language)

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hello, I am very new to metatables so I'm sorry if this is wrong but, here's how I would've done it, I think.

local mt = setmetatable(leaderstats:GetChildren(), {
    __index = function(tab)
        coins.Value = 1
        multiplier.Value = 1
        rebirths.Value = 0
        steps.Value = 1

        return "Some data in leaderstats is nil!"
    end,
})

for i, v in ipairs(mt) do
    if not v then --checks if v is nil
        print(mt[i]) --prints the return
    end
end
0
Sorry but your code doesn't work SpectacularPavlos 86 — 3y
Ad

Answer this question