在RLua中保存播放器数据

时间:2016-05-14 17:21:33

标签: lua roblox

有没有理由说这些不起作用?

玩家加入剧本:

local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")


game.Players.PlayerAdded:connect(function(player)

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local points = Instance.new("IntValue", stats)
    points.Name = "Points"

    local credits = Instance.new("IntValue", stats)
    credits.Name = "Credits"    

    local key = "player-"..player.userId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        --Save format: (points, credits)
        points.Value = savedValues[1]
        credits.Value = savedValues[2]
    else
        local ValuesToSave = {points.Value, credits.Value}
        DataStore:SetAsync(key, ValuesToSave)
    end


end)

播放器离开时的另一个脚本。

local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")

game.Players.PlayerRemoving:connect(function(player)

    local key = "player-"..player.userId    

    --Save key: {points, credits}
    local valuesToSave = {player.leaderstats.Points.Values, player.leaderstats.Credits.Values}
    DataStore:SetAsync(key, valuesToSave)

end)

这适用于我正在处理的游戏,如果您不知道,则RLuaRoblox Lua

1 个答案:

答案 0 :(得分:0)

是的,在你有机会提取数据之前,很可能会删除领导者。

我建议不要使用leaderstats作为数据的参考。最好直接在脚本上存储数据。

但是,如果你真的必须使用leadertats,请将其托管到其他地方,提取数据,然后删除它。

'(.+) ' + text + ' (.+)'

或者你可以在它们被重新定义之前在变量中定义所有这些对象。

但同样,我强烈建议不要使用leaderstats来保存数据。 Exploiters可以轻松更改数据并将值更改为高数字。

相关问题