Leaderstats Datastore Not Working

For some reason this datastore is not working for me even when I play the game out of studio. It says the Data Saves but when I rejoin it does not save the value. If anyone has any idea why I would appreciate the help. Thanks.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreSky") 

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats

local Diamonds = Instance.new("IntValue")
Diamonds.Name = "Diamonds"
Diamonds.Parent = leaderstats

local playerUserId = "Player_"..player.UserId
    
local data
local success, errormessage = pcall(function()
    data = DataStore:GetAsync(playerUserId)
end)
    
if success and data then
    Clicks.Value = data[1]
    Diamonds.Value = data[2]
else
    print("Data not found")
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId

local data = {
    player.leaderstats.Clicks.Value; 
    player.leaderstats.Diamonds.Value    
}



local success, errormessage = pcall(function()
    DataStore:SetAsync(playerUserId, data)
end)
    
if success then
    print("Data saved")    
else
    print("Error in saving data")
    warn(errormessage)
end
end)

Make sure that you’re changing the leaderstat value objects on the SERVER (script), not the client (localscript). Can we see the code you’re using to set the leaderstat value objects?

local Clicks = game.Players.LocalPlayer.leaderstats.Clicks

ButtonClick.MouseButton1Click:Connect(function()
	Clicks.Value = Clicks.Value + 1
	if Clicks.Value >= 1 then
		ClickNum.Text = Clicks.Value
	end
end)

This is the code I am using to set the leaderstats

So I need to change the leaderstats by Firing the server and telling the server to change it for it to work?

1 Like

Yes. LocalScripts don’t change the value for the server’s end so you’ll need to tell the server to change it for you.

1 Like

And how do i do it exactly? cause it doesnt work for me