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)
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.
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.
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.
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.
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.