Why isn't my leaderstat saving?

When I change the value of donated (in another script), after I leave it goes back to zero, why is this?

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local playerSaves = dataStoreService:GetDataStore("PlayerSaves")


players.PlayerAdded:Connect(function(player)
	
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player
	local kills = Instance.new("NumberValue")
	kills.Name = "Kills"
	kills.Parent = stats
	local donated = Instance.new("NumberValue")
	donated.Name = "Donated"
	donated.Parent = stats
	local wins = Instance.new("NumberValue")
	wins.Name = "Wins"
	wins.Parent = stats
	local winstreak = Instance.new("NumberValue")
	winstreak.Name = "Winstreak"
	winstreak.Parent = stats
	local deaths = Instance.new("NumberValue")
	deaths.Name = "Deaths"
	deaths.Parent = stats
	
	local function charAdded(char)
		
		local humanoid = char:WaitForChild("Humanoid")
		
		humanoid.Died:Connect(function()
			
			deaths.Value += 1
			
			local killer = humanoid:FindFirstChild("killerTag")
			
			if killer and killer.Value then
				killer.Value.leaderstats.Kills.Value += 1
			end
		end)
	end
	
	if player.Character then
		charAdded(player.Character)
	end
	
	player.CharacterAdded:Connect(charAdded)
	
	local data = playerSaves:GetAsync(player.UserId)
	
	if data then
		
		for name,value in pairs(data) do
			
			local val = stats:FindFirstChild(name)
			
			if val then
				
				val.Value = value
				
			end
			
		end
		
	end
	
end)

players.PlayerRemoving:Connect(function(player)
	
	local stats = player:FindFirstChild("leaderstats")
	
	if stats then
		
		local data = {}
		
		for _,stat in pairs(stats:GetChildren()) do
			data[stat.Name] = stat.Value
		end
		
		playerSaves:SetAsync(player.UserId,data)
		
	end
	
end)

game:BindToClose(function()
	wait(3)
end)

Are you changing the value in a local script or server script?

The issue might be due to the values not replicating.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.