DataStore won't work at all

I took out the pcalls and still, no errors

Nope, that only means something if you are using loadstring() in your code, but you’re not so It doesn’t matter.

Is there something else to do besides allowing API in Configure Game and enabling API from game settings through studio to make DataStore work ?

Did you try putting the functions back in pcalls and try using a local variable to store the cash and xp?

Pcall is a callback, this is a non-issue. If this was a problem, it’d throw for using a return. Note how the result is printed to the console via warn if success is false.

Guess I really need to write that pcall tutorial more than ever.

	local Data1
	local successCash, returnCash = pcall(function()
		Data1 = DataStore:GetAsync(player.UserId..'Money')
	end)
	
	local Data2
	local succesLvl, returnLvl = pcall(function()
		Data2 = DataStore:GetAsync(player.UserId.."Level")
	end)
	
	if successCash then
		MoneyInt.Value = Data1
	else
		warn(returnCash)
	end
	
	if succesLvl then
		LevelInt.Value = Data2
	else
		warn(returnLvl)
	end

Nope, still doesn’t work

Please reference what I asked beforehand. Doing debugging is very crucial here. I am unsure whether this is an implementation error or something else right now.

1 Like
function playerAdded(player)
	local DataFolder = Instance.new("Folder")
	DataFolder.Parent = player
	DataFolder.Name = "leaderstats"
	
	local MoneyInt = Instance.new("IntValue")
	MoneyInt.Parent = DataFolder
	MoneyInt.Name = "Money"
	
	local LevelInt = Instance.new("IntValue")
	LevelInt.Parent = DataFolder
	LevelInt.Name = "Rebirths"
	
	local Data1
	local successCash, returnCash = pcall(function()
		Data1 = DataStore:GetAsync(player.UserId..'Money')
	end)
	
	local Data2
	local succesLvl, returnLvl = pcall(function()
		Data2 = DataStore:GetAsync(player.UserId..'Level')
	end)
	
	if successCash then
		print("succes")
		MoneyInt.Value = Data1
	else
		warn(returnCash)
	end
	
	if succesLvl then
		print("yeet")
		LevelInt.Value = Data2
	else
		warn(returnLvl)
	end
end

function playerRemoving(player)
	local sucess, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId..'Cash',player.leaderstats.Money.Value)
		DataStore:SetAsync(player.UserId..'Level',player.leaderstats.Rebirths.Value)
	end)
	
	if sucess then
		print("Succes saving data!")
	else
		warn(errormessage)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoving)

for i, player in pairs(game.Players:GetPlayers()) do
	playerAdded(player)
end

succes, yeet and Success saving data are printed.

Perhaps you need to put them the saving functions in seperate pcalls, that might do the trick.

Also, the for loop is completely unnecessary, it’s just repeating what you already did.

If all of that is printing, then your code works. What’s the specific problem you’re facing here? Your DataStores work.

@mobyboyy The for loop is not unnecessary. That’s a suggestion I made.

When I run the game, and change the value of the Int, leave, and rejoin, the value is 0.
Both in the actual game and studio.

You should try this plugin to see if your datastore is actually saving, it’s without a doubt a lifesaver.

…just to be clear, you do change to the server view to change data in Studio, right? In a Studio test session, you start from the client view, so changes to data naturally won’t save because the server doesn’t see the change.

Your code works and there’s no issues. The prints run. APIs are toggled. This is the last thing I’m thinking of. You’re changing data from the client, not the server, therefore it’s not saving.

1 Like

That’s actually a great suggestion, he might’ve done that.

That actually worked, thanks !