Datastore Script not working

but you i’m all ear if can tell me what i can improve

local function PlayerAdded(Player) -- the player you don't have to say game.Players:FindFirstChild(Player.Name)
      local leaderstats = Instance.new("Folder", Player)
      leaderstats.Name = "leaderstats"
end
game.Players.PlayerAdded(PlayerAdded)

That are a few very intriguing changed, i’ll make sure to implement them in my future projects, thanks

A good way to fix this is to put it in the BindToClose instead of player removing. Sometimes on studio the server shuts down but that code executes. Another way is to switch to server mode when hitting stop. Then it would work as well.

He already has a BindToClose function? also BindToClose shouldn’t be used in studio.

can you explain more the second method?

I disagree. Maybe I misread the code but he only saves the data here: Player.PlayerRemoving:connect(SaveData). Bind to close works best in studio because you can then see the data save as well as make it save. In a real game it would work fine, but in studio there can be some errors. Similar to here maybe. Why does SetAsync break the code?

So basically you are calling the save data function in the bind to close as well, because on studio, the game is shutting down because you are the last person in that server

Even on the BindToClose api it says if RunService:IsStudio() then return end

Also there is no point in saving data twice. Per say PlayerRemoving does save the data then bindtoclose will do the same.

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

This is what I do in my game

game:BindToClose(function()
	
	if RunService:IsStudio() then 
		return 
	end
	
	task.wait(5) -- Per say you shutdown all your servers it will wait 5 seconds before actually shutting down the server
	
end)

Well it could be that i get that wrong, but i’m not worried about the datastores also working in Studio, its more that the last value of the 3 which should get saved donesn’t get saved

Are you really updating the value, because I’ve copied your whole code and pasted it into a script and then I changed all the values and they successfully saved.

Edit 1: yes all the values including the boolvalue

Edit 2: go to game settings - security. Do you have Enable Studio Access to API Services enabled? @Agusta0002

very weird, i’ll look into it another time

Probably because the game shuts down before it saves. This is well explained in the solution to the post I linked. I am assuming this because @Arthur_Grandwell tested it and it worked. Make the game public and try it on roblox. Let me know if I have misunderstood though please.

I think he has Enable Studio Access to API Services disabled.

i have it enabled, don’t worry

https://www.roblox.com/games/8293431748/Private-USB-Dev-place

Here you go

I think it was on friends only, i’ve made it private and public again

I would reccomend creating leaderstats in a seperate script, personally. Try this tutorial, it worked well for me. ROBLOX Studio Leaderstats with Data Store Tutorial - YouTube

Please give him a propper reason as to why you should do that?

It is super unnecessary and if you want to efficiently save data you should use UpdateAsync()

Edit 1: you only need like 100 lines for a good datastore system so?

It makes the code less long and easier to read?