Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Save/Load Data not working correctly?

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.

2 comments

0

I have done some reading and this seems like it's happening to a lot of people. People that know what they are doing more than myself were saying it just started today. Still, if anyone sees an issue here please let me know!

0

This happens to me to, it might be a problem with roblox itself.

1 answer

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)

1 comment

0

Am I missing something here? I've looked over this and as far as I can see the only difference is you're connecting the function a different way that still works. Neither methods (mine or yours) have been successful in saving the leaderstats values.

This discussion is preserved as it was. New answers, comments, and votes are closed.