所以我遇到了数据库问题,没有为玩家创建个人资料,导致加入时没有发生任何变化
我使用的数据库代码来自 youtube 教程,在我把头撞到墙上几个小时后,它仍然不起作用,这里有什么问题,为什么它会失败并制作
local PlayerDataHandler = {}
local dataTemplate = {
Cash = 0,
Inventory = {}
}
local ProfileService = require(game.ServerScriptService.ProfileService)
local Players = game:GetService("Players")
local ProfileStore = ProfileService.GetProfileStore(
"PlayerProfile",
dataTemplate
)
local Profiles = {}
local function playerAdded(player)
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
if profile then
profile:AddUserId(player.UserId)
profile:Reconcile()
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick()
end)
if not player:IsDescendantOf(Players) then
profile:Release()
else
Profiles[player] = profile
end
else
player:Kick()
end
end
function PlayerDataHandler:Init()
for _, player in game.Players:GetPlayers() do
task.spawn(playerAdded, player)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(player)
if Profiles[player] then
Profiles[player]:Release()
end
end)
end
local function getProfile(player)
assert(Profiles[player], string.format("Profile doesn't exist for player"))
return Profiles[player]
end
function PlayerDataHandler:Get(player, key)
local profile = getProfile(player)
assert(profile.Data[key], string.format("Data doesn't exist for okey: %s", key))
return profile.Data[key]
end
function PlayerDataHandler:Set(player, key, value)
local profile = getProfile(player)
assert(profile.Data[key], string.format("Data doesn't exist for okey: %s", key))
assert(type(profile.Data[key]) == type(value))
profile.Data[key] = value
end
function PlayerDataHandler:Update(player, key, callback)
local profile = getProfile(player)
local oldData = self:Get(player, key)
local newData = callback(oldData)
self:set(player, key, newData)
end
return PlayerDataHandler
如果您查看错误下方的蓝线,您可以追溯到出现此错误的脚本。 该错误基本上是告诉您它找不到您要查找的玩家数据的键,或者您在使用 :Get 、 :Set 或 :Update 函数时输错了键的名称(一如既往地区分大小写)(我不知道哪个)来自您给我的一行错误中的一个)或者您只需要给它几分钟(这就是我的结果)。 我不知道你为什么在堆栈溢出上发布这个,而是使用 roblox 开发论坛,我也不知道为什么我在 9 个月后回复