Attempt to call a nil value when saving datastores

is that local script or server script?

It’s a serverscript, both of the scripts are in serverscriptservice

if you used my script solution!
did you get any output message?
like warning,error ot print messages

nope, it shows the same attempt to call a nil error in the output

hi sorry i just saw something wait ill change the above script
try the above script now

(my solution in case any confusion)

also change the adding cash script to:

game.Players.PlayerAdded:Connect(function(plr)
	local value = plr.Currency
	while wait(1) do
		value.Value+=25
	end
end)

you were creating coppies of currency number valuses before

Basically the issue you are occuring is that either :GetAsync or :SetAsync does not succeed the first try. This is an issue with StarterGui:SetCore and :GetCore as well, you can fix this by retrying until it works.

Please use the following function:

local globalDataStoreCall do
   local MAX_RETRIES = 8

   local RunService = game:GetService('RunService')

   function globalDataStoreCall(method, key, ...)|
      local results = {}
      for attempt = 1, MAX_RETRIES do
         results = {pcall(myPlayerDataStore[method], myPlayerDataStore, key, ...)}
         if results[1] then
            break
         end
         RunService.RenderStepped:Wait()
      end
      return results
   end
end

What you will do next is replace :GetAsync and :SetAsync with the following:

globalDataStoreCall("GetAsync", player.UserId)
globalDataStoreCall("SetAsync", player.UserId, player.Currency.Value)

do you did your datastorescript with tutorial?
because if you don’t you can simply follow a tutorial on this - it’s better then be stuck on something for hours.
anyway I’m using datastorescript in almost all of my games so that’s the code:

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

local function LeaderboardSetupData(player)
    --creates coins data
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = player
	
	local CoinsData
	local success, errormessage = pcall(function()
		CoinsData = myDataStore:GetAsync(player.UserId.."-Coins")
	end)
	if success then
		coins.Value = CoinsData
	else
		print("error getting data:")
		print(errormessage)
	end

end

local function SaveData(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-Coins",player.Coins.Value)
	end)

	if success then
		print("data saved!")
	else
		print("error saving data:")
		print(errormessage)
	end
end

Players.PlayerAdded:Connect(LeaderboardSetupData)
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(SaveData)

Am I able to enter in a wait script that adds cash every 5 minutes in this script, if so, where should I put it?

in other script.
you can call something like this:
make sure to use it in normal script, not local:

while wait(300) do
  for _, player in pairs(game.Players:GetChildren()) do
     player.Coins.Value += 5 -- you can change it to whatever number you want
  end
end

You are not setting up the “starter” value correctly, this is the issue when you are trying to add cash to the value.
Add this to your current script, anything under the while loop will not run.

Have the following code at the end of your script:

local PlayerService = game:GetService("Players")
local AddCurrency = 25

while wait(5 * 60) do -- 5 minutes
   for _, player in pairs(PlayerService:GetPlayers() do
     if player:FindFirstChild("Currency") then
         player.Currency.Value = (player.Currency.Value ~= nil and player.Currency.Value + AddCurrency) or AddCurrency
      end
   end
end
1 Like

where do I add the function inside the script?

1 Like

Just tried, one of your in-built print messages said error saving data

This seemed to work, also is there a way I can add a local script for a startergui that lists the current cash in a textlabel?

yes.
add a gui in the startergui and then a label and then add this script(local script) to the label:

local text = script.Parent
local player =  game.Players.LocalPlayer
text.Text = "Cash: "..player.Coins.Value
player.Coins.Changed:Connect(function()
	text.Text = "Cash: "..player.Coins.Value
end)

Thank you bro, everything works now!

so put my post as the solution

yeah i did that before already

@vxsqi i didn’t refresh the page :sweat_smile: