Save a leaderstats (Script)

One warning: Data stores cannot be tested in Studio, unless you have Studio API checked.

Should be looking like this in the settings page:
Enable Studio Access to API Services: [ ]

Else, it will error.

1 Like

I have set true Api services just i d’ont have leaderstats whit you script 


1 Like

@Toadiant

The instance is named below the creation of the donate IntValue. The highlighted line of code is bad practice anyway, setting properties after the object’s been attached to the DataModel is inefficient if done where not needed to.


@Operatik

Please don’t put the code that defines the function of Donate.OnServerInvoke in the PlayerAdded event. Every time a player joins, the function gets overwritten with a new one.

Also:

That’s in your recent code. Two lines below it, you parent the value again. Just wanted to point that out for you. :wink:


@Med367367

Just a note that while reading through this thread, you should also be trying to learn and take some lessons from the code you’re getting. Scripting Support is not necessarily a place to ask for free code. There are some issues that you can debug yourself; please don’t copy and paste a script into Studio, then come back without attempting any fixes and say that it’s not working.

Also, in terms of getting leaderstats to show, I think you just need to change the order in which you’re parenting things. Instead of parenting the stats folder immediately after creating it, parent it after you’ve created and parented the rest of your data values.

4 Likes

I do not understand “” Could you (if you do not mind) translate this into “Frentch” because I do not have to understand

Don’t know French myself but I consider Google Translate a great friend. You should be able to get a general idea from this translation:

Notez simplement qu’en lisant ce fil, vous devriez Ă©galement essayer d’apprendre et de tirer quelques leçons du code que vous obtenez. L’assistance de script n’est pas nĂ©cessairement un endroit pour demander un code gratuit. Il existe certains problĂšmes que vous pouvez rĂ©soudre vous-mĂȘme. veuillez ne pas copier et coller un script dans Studio, puis revenir sans essayer de solutionner et dire que cela ne fonctionne pas.

De plus, s’agissant de l’affichage des dirigeants, je pense que vous devez simplement modifier l’ordre dans lequel vous dirigez les choses. Au lieu de gĂ©rer le dossier de statistiques immĂ©diatement aprĂšs l’avoir crĂ©Ă©, parentz-le aprĂšs avoir crĂ©Ă© et parentĂ© le reste de vos valeurs de donnĂ©es.

I tried a different solution before coming on the form I just do not know how to do it anymore

i patch my problem code is
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-points")
		
	end)
	if success then
		Points.Value = data
else
	print("ERROR DATA NOT SAVED")
	warn(errormessage)
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
		
	end)
	if success then
		print("Player data saved !")
	else
		print("Error data not saved")
	end
	
end)

For future posts please follow the guidelines: