在 Roblox 排行榜中保存 2 个玩家值

问题描述 投票:0回答:1

我一直在尝试为我正在开发的游戏保存 2 个值,但由于某种原因我无法让脚本运行。

这是脚本 -

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local SufferingsStore = DataStoreService:GetDataStore("Sufferings")
local SufferCoinsStore = DataStoreService:GetDataStore("SufferCoins")

local function leaderboard(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local SufferCoins = Instance.new("IntValue")
SufferCoins.Name = "SufferCoins"
SufferCoins.Parent = leaderstats

local Sufferings = Instance.new("IntValue")
Sufferings.Name = "Sufferings"
Sufferings.Parent = leaderstats

local data1, data2
local success, errorMessage = pcall(function()
    data1 = SufferCoinsStore:GetAsync(player.UserId.. "-SufferCoins")
    data2 = SufferingsStore:GetAsync(player.UserId.. "-Sufferings")
end)

if success then
    SufferCoins.Value = data1
    Sufferings.Value = data2
    print("Data succesfuly loaded for " .. player.Name)
else
    warn(errorMessage)
end
end

local function saveData(player)
local success, errorMessage = pcall(function()
    SufferCoinsStore:SetAsync(player.UserId.. "-SufferCoins", player.leaderstats.SufferCoins.Value)
    SufferingsStore:SetAsync(player.UserId.. "-Sufferings", player.leaderstats.Sufferings.Value)
end)

if success then
    print("Data Saved for " .. player.Name)
else
    print("An Error Occurred While Saving for " .. player.Name)
    warn(errorMessage)
end
end

Players.PlayerAdded:Connect(function(player)
leaderboard(player)

player.CharacterAdded:Connect(function()
    leaderboard(player)
end)
end)

Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)

for _, player in pairs(Players:GetPlayers()) do
leaderboard(player)
end

我尝试观看 Youtube 教程,询问 ChatGPT(曾经很有帮助),我希望数据能够保存。该脚本最终没有做太多事情,只是打印出它加载/保存了数据,而没有实际这样做。

lua datastore leaderboard
1个回答
0
投票

尝试使用此方法并确保 Roblox API 服务已开启!:

local player = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")

local SufferingsDataStore = dataStore:GetDataStore("Sufferings")
local SufferCoinsDataStore = dataStore:GetDataStore("SufferCoins")

player.PlayerAdded:Connect(function(plr)
    print(plr.Name .." joined")
    
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    
    local Sufferings = Instance.new("IntValue", folder)
    Sufferings.Name = "Sufferings"
    Sufferings.Value = 0

    local SufferCoins = Instance.new("IntValue", folder)
    SufferCoins.Name = "SufferCoins"
    SufferCoins.Value = 0

    local infoSaved = nil
    local infoSaved2 = nil

    local succeful, fail = pcall(function()
        infoSaved = SufferingsDataStore:GetAsync(plr.UserId)
        infoSaved2 = SufferCoinsDataStore:GetAsync(plr.UserId)
    end)

    if succeful then
        plr.leaderstats.Sufferings.Value = infoSaved
        plr.leaderstats.SufferCoins.Value = infoSaved2
        warn("Data loaded successfully")
    end
end)

player.PlayerRemoving:Connect(function(plrE)
    warn("Goodbye " ..plrE.Name.."!")
    local points1 = plrE.leaderstats.SufferCoins.Value
    local points2 = plrE.leaderstats.Sufferings.Value

    local succeful, fail = pcall(function()
        SufferingsDataStore:SetAsync(plrE.UserId, points2)
        SufferCoinsDataStore:SetAsync(plrE.UserId, points1)
    end)

    if succeful then
        warn("Data saved correctly")
    end
end)

希望对您有用,再见!

© www.soinside.com 2019 - 2024. All rights reserved.