Leaderstats glitching?

Im making a game but when I buy something with the money and then pick up a coin the money will go back to what it was before?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSave")

local function SavePlayerData(player)

	local success,errormsg = pcall(function()

		local SaveData = {}

		for i,stats in pairs(player.leaderstats:GetChildren()) do

			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId,SaveData)
	end)

	if not success then
		return errormsg
	end			
end	

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character) --Check if player's Character is added
		player.Character.Humanoid.Died:Connect(function()
			local errormsg = SavePlayerData(player)

			if errormsg then	
				warn(errormsg)		
			end	
		end)
		local Stats = Instance.new("Folder")
		Stats.Name = "leaderstats"
		Stats.Parent = player

		local statstime = Instance.new("IntValue")
		statstime.Name = "Money"
		statstime.Parent = Stats

		local Data = SaveDataStore:GetAsync(player.UserId)
		if Data then



			for i,stats in pairs(Stats:GetChildren()) do

				stats.Value = Data[stats.Name]			
			end			
		else				
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)

	local errormsg = SavePlayerData(player)

	if errormsg then	
		warn(errormsg)		
	end	
end)

game:BindToClose(function()
	for i,player in pairs(Players:GetPlayers()) do	

		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)

So can you explain what issue your having? Like define what you mean by “glitching”?

Read the text above the script.

Well then you will need to show the code that you have when you pick up a coin I think as this is just the one to save when the player is added and removed.

db = false
function onTouch(part) 
	local human = part.Parent:findFirstChild("Humanoid") 
	if (human == nil) then return end 
	local plr = game.Players:FindFirstChild(human.Parent.Name)
	if db == false then
		db = true
		plr.leaderstats.Money.Value += 2
		script.Parent.Collect:Play()
		script.Parent.Transparency = 1
		wait(4)
		script.Parent.Transparency = 0
		db = false
	end
end 
script.Parent.Touched:connect(onTouch)

And can you send the code that deals with reducing the amount of coins when the player buys something.

Oh I accidentally was reducing on a local thanks for the help!

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