Data saving isn't working why?

My data saving store isn’t working why? can someone spot the problem It saves but doesn’t load as console says there is no error at all as well it just doesn’t want to load after pcall
local datasavingservice = game:GetService(“DataStoreService”)
local MyDataStore = datasavingservice:GetDataStore(“MyDataStore”)

local MyDataStore = datasavingservice:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)	
	local leaderstats = Instance.new("Folder",Player)
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue",leaderstats)
	Cash.Name = "Cash"
	

	local playerUserId = "Player"..Player.UserId
	--Load Data
	local data
	local success, errormessage = pcall(function()
		data = MyDataStore:GetASync(playerUserId)
	end)
	Cash.Value = data
	if success then
		print("Loaded Data")
		Cash.Value = data
	else
		print("error loading data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local playerUserId = "Player"..Player.UserId
	local data = Player.leaderstats.Cash.Value
	local success, errormessage = pcall(function()
		MyDataStore:SetAsync(playerUserId,data)
	end)
	if success then
		print("Successfully saved data")
		
	else
		print("There was a error while saving data")
		warn(errormessage)
	end
	
end)
1 Like

Well its case sensitive so this should be giving you a error message.

This will not work. UserId is a number. You cant combine numbers and string. (It needs to be turned into string)
use tostring(Player.UserId) in this case.

edit:
use ``` before to make your text look the same as a script.

1 Like

Not necessarily. If you’re concatenating a number with a string, the number will automatically turn into a string.

1 Like

Ive always got issues from that. Perhaps its changed. Idk I tend to use tonumber() and tostring() anyway as it means I know 100% what I’m doing wont cause the issues.

But last I checked it gave me errors.

It doesn’t really matter at the end of the day. Main thing is that the script gets working.

Ive worked with stores a lot so ive made functions that can do all kinds of things like save an infinite list to a datastore or save models and objects. Id say it would be worth making some universal function @runaredie. Start small and build it up.

Thanks for telling me about ``` that should help me a bunch in the future appreciate it but some reason the script still isn’t working it is just saying data saved but never data loaded and this is a server script
edit: data saving is new to me

Check what is being saved. it could be that whatever it is saving is becoming nil so when you try to get the value it will also be nil.

print off what is being saved to double check it and ensure there is current save data

Edit:

Here’s what I use for all of my data stores. Pull it apart and use bit if needed. Its a freebie. Your script is very similar in ways so you could use it to look for minor issues.

(Im going offline so that’s why im just posting this up)

You can test it by yourself, use this in the command bar:

local number = 2
local string = "dwwwwwadsa"

local stringToBePrinted = string .. number
print(stringToBePrinted, type(stringToBePrinted))

And like you said, it doesn’t really matter at the end of the day since not using tostring() just saves you some time in writing your script.

I’m already aware, I read through the APIs to catch up on these changes. It is irrelevant to this topic.

1 Like

Nothing doesn’t print at all for the saving even though I try to print the data no nil at all

Try this:

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

local Players = game:GetService("Players")

local DefaultCash = 0

Players.PlayerAdded:Connect(function(Player)	
  local playerKey = "User-" .. Player.UserId

  local leaderstats = Instance.new("Folder")
  leaderstats.Name = "leaderstats"

  local Cash = Instance.new("IntValue")
  Cash.Name = "Cash"
  Cash.Value = DefaultCash

  local data
  local success, errormessage = pcall(function()
    data = MyDataStore:GetAsync(playerKey)
  end)

  if success then
    if data then
      warn("Sucessfully loaded " .. Player.Name .. "'s data!")
      Cash.Value = data.Cash or DefaultCash
    end
  else
    warn("There was an error when loading " .. Player.Name .. "'s data. DataStore message " .. errormessage)
  end

  leaderstats.Parent = Player
  Cash.Parent = leaderstats
end)

Players.PlayerRemoving:Connect(function(Player)
  local playerKey = "User-" .. Player.UserId

  local leaderstats = Player:FindFirstChild("leaderstats")
  if not leaderstats then
    return
  end

  local Cash = leaderstats:FindFirstChild("Cash")
  if not Cash then
    return
  end

  local CashValue = Cash.Value

  local success, errormessage = pcall(function()
    MyDataStore:SetAsync(playerKey, {
      Cash = CashValue
    })
  end)

  if success then
    warn("Successfully saved " .. Player.Name .. "'s data")
  else
    warn("There was an error while saving " .. Player.Name .. "'s data. DataStore message: " errormessage)
  end
end)

I think I know, the tostring() didnt matter before as it was automatically doing it

The datastore can only save string

Its not nil but numbers and a DS cannot save numbers.

try using tostring(Data) and make sure that playerUserId is still a string (check its the same). My concern is that its saving to a different name as .Joined and .Removing are two different events and playerUserId is a local var

Try this

local MDT = DTS:GetDataStore("CurrencyDT") -- Change to name of your data store
local PS = game:GetService("Players")
local currencyName = "Currency" -- change to currency name

game.Players.PlayerAdded:Connect(function(plr) -- Runs everytime a player joins
	local Folder = Instance.new("Folder")
	Folder.Name = "leaderstats"
	Folder.Parent = plr
	
	local Value = Instance.new("IntValue")
	Value.Name = currencyName
	Value.Parent = Folder
	
	local ID = currencyName.."-"..plr.UserId
	
	local savedData = nil
	pcall(function()
		savedData = MDT:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		Value.Value = savedData
		print(plr.Name.."'s data has loaded!")
	elseif savedData == nil then
		Value.Value = 200
		print(plr.Name.." is a new player!")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr) -- runs everytime a player is being removed from the server
	local ID = currencyName.."-"..plr.UserId
	
	MDT:SetAsync(ID, plr.leaderstats[currencyName].Value)
end)

game:BindToClose(function() -- When the server is ready to shut down it stops it till this function has completed
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This server is shutting down! Your data should be saved!")
		end
	end
	
	wait(5)
end)

Don’t change anything about the script unless it says to

1 Like

I have both of them to tostring()
this is how the script looks like now

local datasavingservice = game:GetService("DataStoreService")
local MyDataStore = datasavingservice:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)	
	local leaderstats = Instance.new("Folder",Player)
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue",leaderstats)
	Cash.Name = "Cash"
	
	while wait(120) do
		Cash.Value = Cash.Value + 100
	end
	local playerUserId = tostring("Player"..Player.UserId)
	--Load Data
	local data = nil
	local success, errormessage = pcall(function()
		data = MyDataStore:GetAsync(playerUserId)
	end)
	print(data)
	if success then
		print("Loaded Data")
		Cash.Value = data
	else
		print("error loading data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local playerUserId = tostring("Player"..Player.UserId)
	local data = Player.leaderstats.Cash.Value
	local success, errormessage = pcall(function()
		MyDataStore:SetAsync(playerUserId,data)
	end)
	if success then
		print("Successfully saved data")
		
	else
		print("There was a error while saving data")
		warn(errormessage)
	end
	
end)

The while loop will stop any code that goes below it from executing. Try using coroutines.

1 Like

Data here is going to be a number value. That wont save.
Good corrections on the other stuff though.

1 Like

AW MAN how did none of us seen this

Oh I didn’t include this in the original script dumb of me

Change this:

while wait(120) do
		Cash.Value = Cash.Value + 100
	end

to this:

coroutine.wrap(function()
while wait(120) do
		Cash.Value = Cash.Value + 100
	end
end)()
2 Likes

This is necessary @runaredie. The while loop will stop your script from running any further.

Nice spotting Giggio

2 Likes

I’m going to go to dinner so Imma be afk, I’ll come back and try to help if the issue hasn’t been fixed.
Edit: nvm

1 Like