Datastore not saving

I have this leadestats script with saving using datastores but it doesn’t save and always warn me that there was an error saving the data which i scripted so that i know that it doesn’t save

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr 
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = plr
	
	local data 
	local cash
	local success, errormessage = pcall(function()
		
		data = myDataStore:GetAsync(plr.UserId.."-Level")
		cash = myDataStore:GetAsync(plr.UserId.."-Cash")

	end)
	if success then
		Level.Value = data 
		Cash.Value = cash
	else
		warn(errormessage)
		print("There was an error saving data")
	end
end)


game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(plr.UserId.."-Level", plr.leaderstats.Level.Value)
		myDataStore:SetAsync(plr.UserId.."-Cash", plr.Cash.Value)
	end)
	
	if success then
		print("Player's Data successfully saved")
	else
		print("There was an error saving the data")
		wait(errormessage)
	end
	
	
end)

Output:

‘’’
There was an error saving the data
‘’’

It always says there is an error saving a data its not saving anything

Is two leaderstats in one script with datastores not allowed should i do it seperately

You might want to have 2 different data stores to store 2 different variables. Also you might want to use UpdateAsync instead of SetAsync

1 Like

i used 2 datastores look at the script

Its still says that there was an error saving the data

Did you know which print it errors at?

leaderstats should be its parent

But i want it to show in a different gui not the leaderstats

Hello, did you test this in Roblox Studio?

You can just create another folder, and have your cash stored in there (the name should be something other then leaderstats). I think its cleaner then having it directly in the player. Anyways can you answer the above question.

1 Like

i thinks its when one of the stats doesn’t save it prints an error

You can use the datamodule like ProfileService : Save your player data with ProfileService! (DataStore Module)

Or you can make your own datamodule.

1 Like

try to use

game.Players.PlayerAdded:connect(function(plr)

I don’t think theres anything wrong with your code. Make sure you have your API services on. Also, since your cash is not a parent of leaderstats it wont show on the tab thingy.

However it still exists. Not sure why you’re getting those errors.

1 Like

Also you should read this : DataModel | Documentation - Roblox Creator Hub

its still says there was an error saving the data i used different folder with different name fore the parent of cash

Its working now i recieved an error because i quit to early and i think some things are not loaded yet thank you for helping me

1 Like

Hey, this important you should use this.

1 Like

yeah i will use it in case i need to shutdown servers so that players data is save thank you for reminding me

This is not what I mean, if you are testing from the studio and if you press the stop button the game will suddenly close and your actions will not be performed.

1 Like