I am finishing up a game I have been working on for a few weeks, I had the load/save data working properly but now when I test my game the data will not save I believe. Whenever I leave the game the console says "attempt to index nil with Cash" even though the Cash value (the nil) does exist. Am I not calling the value correctly? Thanks in advance!
local DataStoreService = game:GetService("DataStoreService") local CashStore = DataStoreService:GetDataStore("Cash") local KillsStore = DataStoreService:GetDataStore("Kills") local player = game.Players.LocalPlayer local NPC = game.Workspace.NPC:WaitForChild("Humanoid") local STARTING_CASH = 100 local function onPlayerAdded(player) local playerKey = "Player_" .. player.UserId wait() print("User ".. player.UserId.. " Joined") local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = leaderstats local Kills = Instance.new("IntValue") Kills.Name = "Kills" Kills.Parent = leaderstats local myCash local success, err = pcall(function() myCash = CashStore:GetAsync(playerKey) or STARTING_CASH end) if success then Cash.Value = myCash print("loading "..Cash.Value) else print("failed to retrieve Cash") warn(err) end local myKills local success, err = pcall(function() myKills = KillsStore:GetAsync(playerKey) or 0 end) if success then Kills.Value = myKills print("loading "..myKills) else print("failed to retrieve Kills") warn(err) end
This is the part of the script that loads the data. I don't think anything is wrong with this part but I could be wrong.
local function onPlayerLeft(player) wait() local playerKey = "Player_" .. player.UserId local myCash = player.leaderstats.Cash.Value print(myCash) local success, err = pcall(function() myCash = CashStore:SetAsync(playerKey) end) if success then print("User "..playerKey.." saved with "..myCash.." in the bank") else print("error") warn(err) end local myKills = player.leaderstats.Kills.Value print(myKills) local success, err = pcall(function() myKills = KillsStore:SetAsync(playerKey) end) if success then print("User "..playerKey.." saved with "..myKills.." in the bank") else print("error") warn(err) end end game.Players.PlayerRemoving:Connect(onPlayerLeft)
This is the part that saves stats, or it is supposed to. Again, the console prints "attempt to index nil with Cash". This will be my first game so I still haven't figured this all out quite yet. Save and load data are confusing to me.
Hello, you did not pass arguement in second script, this can be the problem. Here is repaired version:
game.Players.PlayerRemoving:Connect(function(player) wait() local playerKey = "Player_" .. player.UserId local myCash = player.leaderstats.Cash.Value print(myCash) local success, err = pcall(function() myCash = CashStore:SetAsync(playerKey) end) if success then print("User "..playerKey.." saved with "..myCash.." in the bank") else print("error") warn(err) end local myKills = player.leaderstats.Kills.Value print(myKills) local success, err = pcall(function() myKills = KillsStore:SetAsync(playerKey) end) if success then print("User "..playerKey.." saved with "..myKills.." in the bank") else print("error") warn(err) end end)