Код IT
← Каталог

Практикум — обби на Roblox — Рекурсивное копирование и API `get` / `set` / `increment`

Фрагмент из «Практикум — обби на Roblox»: Рекурсивное копирование и API `get` / `set` / `increment`.

lua spinoffencyclopedia9-04-razrabotka-igr-204 embed URL статья в энциклопедии
Lua main.lua
local function deepCopy(t: { [string]: any }): { [string]: any }
    local copy = {}
    for k, v in pairs(t) do
        copy[k] = if type(v) == "table" then deepCopy(v) else v
    end
    return copy
end

function DataModule.set(player: Player, stat: "Coins" | "Stage" | "Wins", value: number)
    local data = session[player]
    if not data then return end
    data[stat] = value
    local ls = player:FindFirstChild("leaderstats")
    local iv = ls and ls:FindFirstChild(stat) :: IntValue?
    if iv then iv.Value = value end
end
local function deepCopy(t: { [string]: any }): { [string]: any }
    local copy = {}
    for k, v in pairs(t) do
        copy[k] = if type(v) == "table" then deepCopy(v) else v
    end
    return copy
end

function DataModule.set(player: Player, stat: "Coins" | "Stage" | "Wins", value: number)
    local data = session[player]
    if not data then return end
    data[stat] = value
    local ls = player:FindFirstChild("leaderstats")
    local iv = ls and ls:FindFirstChild(stat) :: IntValue?
    if iv then iv.Value = value end
end