【问题标题】:Roblox DataStore wont save or even remember my UserIdRoblox DataStore 不会保存甚至记住我的 UserId
【发布时间】:2021-11-03 23:15:33
【问题描述】:

所以我一直在制作 Roblox Evolution Simulator 游戏,并且正在尝试制作一个关卡系统。我需要每个级别的最大 Xp 为 1000。如果所有内容的基数均为 0,我可以编写一个简单的脚本,但如果有一个高于 0 的数字,它将无法正常工作!我已经在 Studio 和游戏中尝试过。另外我不使用leaderstat,因为我有一个Xp 栏。 不会保存,我已经浏览了所有可能的论坛页面,但它不起作用! 这是我的代码:

local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name='lead'
    leaderstats.Parent=player
    
    local xp = Instance.new("IntValue", leaderstats)
    xp.Name='Exp'
    
    local level = Instance.new("IntValue", leaderstats)
    level.Name='Level'
    
    local maxExp = Instance.new("IntValue", leaderstats)
    maxExp.Name='Max'
    
    
    local playerUserId = 'Player....'..player.UserId
    local data = playerData:GetAsync(tostring(playerUserId))
    if data  then
        xp.Value = data['Exp']
        print(data["Exp"])
        game.StarterGui.ScreenGui.One.Text=data["Exp"]
        level.Value = data['Level']
        game.StarterGui.ScreenGui.Two.Text=data["Level"]
        print(data["Level"])
        maxExp.Value = data['Max']
        game.StarterGui.ScreenGui.Three.Text=data["Max"]
        print(data['Max'])
    else
        xp.Value=0
        level.Value=0
        maxExp.Value=1000
        game.StarterGui.ScreenGui.Three.Text='Hi'
        print("first time")
    end
    
end

local function create_table(player)
    local player_stats = {}
    for _, stat in pairs(player.lead:GetChildren()) do
        player_stats[stat.name] = stat.value
    end
        return player_stats
end

local function onPlayerExit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserId = 'Player....'..player.UserId
        playerData:SetAsync(tostring(playerUserId), player_stats)
    end)
    
    if not success then
        warn('Could not save data')
    end
end


game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

【问题讨论】:

  • 您能说得更具体一点吗?当您说它不起作用时,您是否看到警告“无法保存数据”并且您是否尝试打印出带有警告的 err 变量?加入游戏时,是不是每次都打印“第一次”?还是没有显示正确的值?

标签: lua roblox


【解决方案1】:

所以有时当最后一个玩家离开服务器时,服务器会关闭,这就是数据没有保存的原因。为了克服这个问题,有一个名为game:BindToClose() 的事件。每当服务器即将关闭时都会触发,现在您从这里连接一个踢所有玩家的功能。 所以你需要在你的代码中添加这个 tp,

game:BindToClose(function()
    for i,v in pairs(game.Players:GetPlayers()) do
        v:Kick("Server Shutdown")
    end
end)

这将确保在服务器关闭之前保存数据。

【讨论】:

  • 这会调用game.Players.PlayerRemoving:Connect吗?
  • 是的,它会调用,因为 BindToClose 只会踢玩家
【解决方案2】:

如果你在 roblox studio 上然后你离开了,你基本上就是在关闭服务器。有一个名为 BindToClose 的函数,它在关机时绑定函数。

game:BindToClose(function()
wait(3) --Waits for player leaving event to fire
end)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2016-09-27
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多