如何保存玩家数据,例如排行榜上的点数?

时间:2018-02-17 15:08:21

标签: lua roblox

如何在排行榜上保存数据,例如点数,这样当玩家重新加入时,他们仍然拥有之前获得的积分数。在我的游戏中,我在我的玩家管理器脚本中创建了一个排行榜,我可以在玩游戏时加入游戏(捕获旗帜游戏)以及当他们捕获旗帜以及它如何在排行榜上显示时会发生什么。我需要在代码中进行编辑以确保玩家在游戏中捕获的数量保存在排行榜上,这样当他们重新加入时,他们所拥有的捕获量数据仍保存在排行榜上。< / p>

我的播放器管理器脚本/排行榜脚本

local PlayerManager = {}

-- ROBLOX Services
local Players = game.Players
local PointsService = game:GetService('PointsService')

-- Game services
local Configurations = require(game.ServerStorage.Configurations)
local Events = game.ReplicatedStorage.Events
local ResetMouseIcon = Events.ResetMouseIcon
local TeamManager = require(script.Parent.TeamManager)
local DisplayManager = require(script.Parent.DisplayManager)

-- Local variables
local PlayersCanSpawn = false
local GameRunning = false

-- Local Functions
local function OnPlayerAdded(player)
    -- Setup leaderboard stats
    local leaderstats = Instance.new('Model', player)
    leaderstats.Name = 'leaderstats'

    local Captures = Instance.new('IntValue', leaderstats)
    Captures.Name = 'Captures'
    Captures.Value = 0

    -- Add player to team
    TeamManager:AssignPlayerToTeam(player)

    player.CharacterAdded:connect(function(character)
        character:WaitForChild('Humanoid').Died:connect(function()
            wait(Configurations.RESPAWN_TIME)
            if GameRunning then
                player:LoadCharacter()
            end
        end)
    end)    

    -- Check if player should be spawned    
    if PlayersCanSpawn then
        player:LoadCharacter()
    else
        DisplayManager:StartIntermission(player)
    end 

end

local function OnPlayerRemoving(player)
    TeamManager:RemovePlayer(player)
end

-- Public Functions

function PlayerManager:SetGameRunning(running)
    GameRunning = running
end

function PlayerManager:ClearPlayerScores()
    for _, player in ipairs(Players:GetPlayers()) do
        local leaderstats = player:FindFirstChild('leaderstats')
        if leaderstats then
            local Captures = leaderstats:FindFirstChild('Captures')
            if Captures then
                Captures.Value = 0
            end
        end
    end
end

function PlayerManager:LoadPlayers()
    for _, player in ipairs(Players:GetPlayers()) do
        player:LoadCharacter()
    end
end

function PlayerManager:AllowPlayerSpawn(allow)
    PlayersCanSpawn = allow
end

function PlayerManager:DestroyPlayers()
    for _, player in ipairs(Players:GetPlayers()) do
        player.Character:Destroy()
        for _, item in ipairs(player.Backpack:GetChildren()) do
            item:Destroy()
        end
    end
    ResetMouseIcon:FireAllClients()
end

function PlayerManager:AddPlayerScore(player, score)
    player.leaderstats.Captures.Value = player.leaderstats.Captures.Value + score
    local success, message = pcall(function() spawn(function() PointsService:AwardPoints(player.userId, score) end) end)
end

-- Event binding
Players.PlayerAdded:connect(OnPlayerAdded)
Players.PlayerRemoving:connect(OnPlayerRemoving)

return PlayerManager

我尝试在不同的脚本上添加一个单独的数据存储但是它不起作用所以我希望我可以在这个脚本中添加一些代码以确保播放器所捕获的数量保存在其上以便播放器可以使用它的捕获来购买游戏中的东西等。

谢谢你和帮助得到赞赏

1 个答案:

答案 0 :(得分:0)

http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore

您可以通过从游戏中获取GlobalDataStore来保存数据,并在加入时使用玩家密钥使用数据存储的GetAsync,然后将点设置为该数据。当他们离开游戏时,您可以使用玩家的密钥将SetAsync设置为GlobalDataStore。如果玩家没有保存任何数据,您可以将其设为0。