2016-05-14 60 views
1

有没有理由为什么这些不起作用?在RLua储存玩家数据

球员加盟脚本:

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,如果你不知道)。

+0

你会分享你期望的代码和它做什么吗? – Piglet

+0

试图澄清。正确的语法 – LoicTheAztec

回答

0

是的,在您有机会提取数据之前,很有可能leadertat被删除。

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

但是,如果您确实必须使用leaderstats,请将其父母带到别处,然后提取数据,然后将其删除。

local lead = player.leaderstats 
lead.Parent = game 
-- extract data 
lead:Destroy() 

或者您可以在变量重新定义前定义所有这些对象。

但是,我强烈建议不要使用leaderstats来保存数据。剥削者可以轻松地更改该数据并将其值更改为较高的值。