DataStore issue

Hello everyone. I know I made a post a while back about a datastore not working but I have a problem with a new one. I believe the problem is with the if statement, but I can’t find the issue. Here is the script.

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“PlayerSaveData”)
local player = game:GetService(“Players”)

player.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild(“leaderstats”)
local currency = leaderstats.Currency
local currencyData

local success,errormessage = pcall(function()
	currencyData = DataStore:GetAsync("currency-"..player.UserId)	
end)

if success then --Nothing past this if statement gets printed
	print("If success")
	if currencyData then
		leaderstats.Currency.Value = currencyData
		print("Currency If Success")
	end
	
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(“currency-”…player.UserId,player.leaderstats.Currency.Value)
print(“Success”)
end)
end)

Thanks for reading.

1 Like
  1. Rename your
local player = game:GetService("Players")

to players

  1. Did you already instance leaderstats and currency?

  2. Are there any errors?

2 Likes

So change all the player variables to players?

1 Like

You said nothing goes by the if success statement so clearly there is a problem with the pcall and you should look into the error message.

Strangely, I’m not getting an error message in the output. The datastore just wasn’t working and I used if statements to find the error. I’m still learning how to program, so I’m not really sure what the issue is.

So you get nil when you try and print out the pcall error message?

Are you testing this in studio or in a live game? Did you enable studio access to datastore services via the game settings modal? This should yield you at least an error message if there is any in your code.

Nothing gets printed in the output past the if statement. I don’t get nil or any value at all past the if statement

I am testing this in studio but it should be working because I’m not getting a message telling me to enable the live datastore in the output. I could be wrong though.

I’m asking what the error message is of the pcall.

local success, ****errormessage**** = pcall(function()
	currencyData = DataStore:GetAsync("currency-"..player.UserId)	
end)

Double check your game settings modal, the output wouldn’t tell you if studio access to datastores are enabled or not. Again, this should yield you an error once enabled if there is any issues present in your code during testing.

Ok it was off. Didn’t realize that was an option. thanks

I’m still learning how scripting works so would the error message be in the output or somewhere else?

local success, errormessage= pcall(function()
	currencyData = DataStore:GetAsync("currency-"..player.UserId)	
end)

warn(errormessage) -- warns the error message

1 Like

Now one of my if statements work, but it is not getting past if currencyData then

Ok. I get a nil value from the error message.

You need to have a default value set for the players Currency value if they don’t have data saved in your game (e.g. if they just started playing for the first time)

if currencyData then
	leaderstats.Currency.Value = currencyData -- existing save detected
	print("Currency If Success")
else
    leaderstats.Currency.Value = 0 -- new player detected
end

This will set the new players’ currency to 0 or whatever integer you would like for them to have when they start playing, hopefully this solves your issue.

1 Like

That solved it. Thanks for everyone helping. I’m still learning and don’t know a lot about scripting so thanks for helping.

1 Like

only the first one. Also I please answer the other questions.

I had already made the leaderstats but didn’t use instancing. The only error I was getting was the one from the pcall. Everything is working now.