Help with DataStoreService

My data isn’t saving for some reason?

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("dataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "Leaderstats" 
	
	local Level = Instance.new("IntValue", Leaderstats)
	Level.Name = "Level"
	
	local Exp = Instance.new("IntValue", Leaderstats)
	Exp.Name = "Exp" 
	
	local data = dataStore:GetAsync(Player.UserId)
	Exp.Value = data
	Level.Value = data

end)

game.Players.PlayerRemoving:Connect(function(Player)
	dataStore:SetAsync(Player.UserId, {Player.Leaderstats.Exp.Value, Player.Leaderstats.Level.Value})
end)
1 Like
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("dataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "Leaderstats" 
	
	local Level = Instance.new("IntValue", Leaderstats)
	Level.Name = "Level"
	
	local Exp = Instance.new("IntValue", Leaderstats)
	Exp.Name = "Exp" 
	
	local data = dataStore:GetAsync(Player.UserId)
	Exp.Value = data.Exp
	Level.Value = data.Level

end)

game.Players.PlayerRemoving:Connect(function(Player)
	dataStore:SetAsync(Player.UserId, {Exp = Player.Leaderstats.Exp.Value, 
 Level = Player.Leaderstats.Level.Value})
end)

Don’t know if its going to work but you are setting the Exp.Value and Level.Value to a table not a value.

Also try use pcalls as if the script errors, it will not break the entire script.

I had the same issue. Add this after your PlayerRemoving Function

game:BindToClose(function()
    for _, client in ipairs(Players:GetPlayers()) do
        save(client)
    end
end)

This should work.

unknown global variable save?..

What do you mean unknown? Is that the error output?

He means he is literally typing this in and is expecting it to work.

im such an idiot sorry lmao HAHAA

Well… It should work. I had the same issue. I used that code and it worked.

What do you mean? I don’t understand.

unknown global variable save
that was the script analysis

If you type in save(client) its going to give you an error. The script does not know what ‘save’ is.

Change the variable to the one in the code and it should work

Yeah, I know. that is why you change it.

I am just giving the template because you should always add a BindtoClose function for datastores.

But he just put that in. He did not change it. Thats why its giving him the error.

Oh ok. So now his job to change it.