I’m trying to store the players data using a script I dont fully understand and keep getting the below error.
18:38:46.342 ServerScriptServices.PlayerManger:85: attempt to index number with ‘Money’ - Server - PlayerManger:85
It seems the value ‘Money’ cant be found but honestly i’m not entirely sure what the error is trying to tell me.
Line 85 has been marked
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local function LeaderboardSetup(value)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = value
money.Parent = leaderstats
return leaderstats
end
local function LoadData(player)
local success, result = pcall(function()
return PlayerData:GetAsync(player.UserId)
end)
if not success then
warn(result)
end
return success, result
end
local function SaveData(player, data)
local success, result = pcall(function()
PlayerData:SetAsync(player.UserId, data)
end)
if not success then
warn(result)
end
return success
end
local sessionData = {}
local playerAdded = Instance.new("BindableEvent")
local playerRemoving = Instance.new("BindableEvent")
local PlayerManager = {}
PlayerManager.PlayerAdded = playerAdded.Event
PlayerManager.PlayerRemoving = playerRemoving.Event
function PlayerManager.Start()
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(PlayerManager.OnPlayerAdded)(player)
end
Players.PlayerAdded:Connect(PlayerManager.OnPlayerAdded)
Players.PlayerRemoving:Connect(PlayerManager.OnPlayerRemoving)
game:BindToClose(PlayerManager.OnClose)
end
function PlayerManager.OnPlayerAdded(player)
player.CharacterAdded:Connect(function(character)
PlayerManager.OnCharacterAdded(player, character)
end)
local success, data = LoadData(player)
print("shouldload")
sessionData[player.UserId] = success and data or {
Money = 0,
UnlockIds = {}
}
local leaderstats = LeaderboardSetup(PlayerManager.GetMoney(player))
leaderstats.Parent = player
playerAdded:Fire(player)
end
function PlayerManager.OnCharacterAdded(player, character)
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
wait(2)
player:LoadCharacter()
end)
end
end
function PlayerManager.GetMoney(player)
return sessionData[player.UserId].Money -------- Line 85
end
function PlayerManager.SetMoney(player, value)
if value then
sessionData[player.UserId].Money = value
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local money = leaderstats:FindFirstChild("Money")
if money then
money.Value = value
end
end
end
end
function PlayerManager.OnPlayerRemoving(player)
SaveData(player, sessionData[player.UserId])
playerRemoving:Fire(player)
end
function PlayerManager.OnClose()
for _, player in ipairs(Players:GetPlayers()) do
PlayerManager.OnPlayerRemoving()
end
end
return PlayerManager
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.