如何使用Datastore保存玩家数值?(Roblox Studio)

huangapple go评论130阅读模式
英文:

How to use Datastore to save player Values? (Roblox Studio)

问题

Kylaaa的建议解决了一些问题,但现在在运行此代码加入游戏后,我得到了一个新的错误。我以前从未使用过数据存储,所以非常感谢所有的帮助,谢谢!

local DataStoreService = game:GetService("DataStoreService")
local UpgradeDataStore = DataStoreService:GetDataStore("UpgradeDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local playerData = {clickValue = 0}

    -- 从数据存储加载数据
    local success, result = pcall(function()
        return UpgradeDataStore:GetAsync(tostring(player.UserId))
    })

    -- 仅在玩家具有先前数据时加载
    if success and result then
        playerData = result
    else
        -- 处理加载错误
        warn("无法为玩家 " .. player.Name .. " 加载数据:" .. errorMessage)
    end

    local clickNum = Instance.new("IntValue")
    clickNum.Name = "clickValue"
    clickNum.Value = clickNum or 1
    clickNum.Parent = player

    -- 当玩家离开时保存点击升级
    player.CharacterRemoving:Connect(function()
        local success, errorMessage = pcall(function()
            playerData.clickValue = clickNum.Value
            UpgradeDataStore:SetAsync(tostring(player.UserId), playerData)
        end)

        -- 处理保存错误
        if not success then
            warn("无法为玩家 " .. player.Name .. " 保存数据:" .. errorMessage)
        end
    end)
end)

现在每次运行此脚本时,我都会收到此错误:ServerScriptService.OnPlayerAdded:56: 试图将字符串与nil连接

英文:

Kylaaa's advice fixed something but now I get a new error in the Output after running this code on joining the game, I've never done datastore before so all the help is very much appreciated thank you!

local UpgradeDataStore = DataStoreService:GetDataStore("UpgradeDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local playerData ={clickValue = 0,}

	-- Load data from DataStore
	local success, result = pcall(function()
		return UpgradeDataStore:GetAsync(tostring(player.UserId))
	end)
	
	-- only load if the player has previous data
	if success and result then
		playerData = result
	else

	-- Handle loading errors
	
		warn("Failed to load data for player " .. player.Name .. ": " .. errorMessage)
		
	end






local  clickNum = Instance.new("IntValue")
clickNum.Name = "clickValue"
clickNum.Value = clickNum or 1
clickNum.Parent = player



-- Save the clicker upgrade when the player leaves
player.CharacterRemoving:Connect(function()
	local success, errorMessage = pcall(function()
		playerData.clickValue = clickNum.Value
		UpgradeDataStore:SetAsync(tostring(player.UserId), playerData)
	end)

	-- Handle saving errors
	if not success then
		warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
	end
end)
end)

Now Everytime this script runs I get this error: ServerScriptService.OnPlayerAdded:56: attempt to concatenate string with nil

答案1

得分: 0

错误提示您,当玩家的角色死亡或从工作区移除时,某种原因导致playerDatanil

当您初始化playerData时,仅在数据存储加载失败时分配一个空表,但您并未考虑到数据存储成功返回空值的情况。您确实应该为其提供一个默认值,以供数据存储覆盖。

-- 使用默认值初始化playerData
local playerData = {
    clickValue = 0,
}

-- 从DataStore加载数据
local success, result = pcall(function()
    return UpgradeDataStore:GetAsync(tostring(player.UserId))
end)

-- 仅在玩家有先前数据时加载
if success and result then
    playerData = result
elseif not success then
    -- 处理加载错误
    warn(string.format("无法为%s加载数据:%s", player.Name, result))
end
英文:

The error is telling you that when the player's character dies or is removed from the workspace, for some reason playerData is nil.

When you initialize playerData, you only assign an empty table if the data store fails to load, but you haven't accounted for the case that the data store successfully returns nothing. You really should give it a default value that the data store overrides.

    -- initialize playerData with default values
    local playerData = {
        clickValue = 0,
    }

    -- Load data from DataStore
    local success, result = pcall(function()
        return UpgradeDataStore:GetAsync(tostring(player.UserId))
    end)

    -- only load if the player has previous data
    if success and result then
        playerData = result
    elseif not success then
        -- Handle loading errors
        warn(string.format("Failed to load data for %s : %s", player.Name, result))
    end

huangapple
  • 本文由 发表于 2023年6月9日 04:05:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定