How to make a button that reset player stats

I’m currently making a GUI that shows player stats and also a button that resets player stats. I made a script that resets, but I’m using Data Store and it makes the stats not reset if the player leave and rejoin the game. Any help will help me. Thanks.

local script inside button that reset stats:

script.Parent.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.leaderstats.Tokens.Value = 0
	game.Players.LocalPlayer.leaderstats.Kills.Value = 0
	game.Players.LocalPlayer.leaderstats.Time.Value = 0
end)

I looked everywhere and couldn’t find any videos or forums.

you are probably changing the value on the client,
but if you updated the value on the server your datastore code will save the updated value

you could try a remote event

you cannot change values via client, use a remote event for that.

make a remote event and insert it into ReplicatedStorage,

replace your local script with this:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

script.Parent.MouseButton1Click:Connect(function()

	RemoteEvent:FireServer() -- fires to server once button clicked

end)

then make a Server Script and place it into
ServerScriptService, then insert this into it:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(plr) -- checks for event, then resets data
       plr.leaderstats.Tokens.Value = 0
	   plr.leaderstats.Kills.Value = 0
	   plr.leaderstats.Time.Value = 0
end)
2 Likes

The stats resets but when I leave, the stats back to how they were before.

I put the RemoteEvent in ReplicatedStorage, renamed RemoteEvent and renamed in scripts

image

image

image

I tried this but didn’t work. My stats back to how they were before when i rejoin.

Are you receiving any errors from your datastore script? If not, did you change anything? Because it might not even be saving at all.

Do you have a data saving script?

If this is the case, you could firstly get the saving script to listen out for the event. Then you could save it, through using the same method, as if they had stats and left the game.

No, I don’t get any warnings or errors about Data Store in the output.
Neither in Client or in Server

image

Yes, i have a data saving script in ServerScriptService; but i didn’t understand “saving script to listen out for the event”.

If you leave the game, does the data automatically save?

if you mean Data Store leaderstats, so yes.

I do not believe errors are related to Data Stores.

Can we see the data saving script?

Do you have the DataStore Editor plug-in? If you do, try checking your saved data and see if it’s still saved as what it was before resetting it.

If nothing is wrong there, can you show me the data saving script?

I will try with DataStore Editor plug-in. Thanks!

1 Like

My data saving script here!

-- Variable
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") 

-- Create folders in Local Player

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Tkn= Instance.new("IntValue", Leaderstats)
	Tkn.Name = "Tokens" 
	Tkn.Value = 0

	local Kills = Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
	
	local Times = Instance.new("IntValue", Leaderstats)
	Times.Name = "Time"
	Times.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Tkn.Value = Data.Tokens 
		Kills.Value = Data.Kills 
		Times.Value = Data.Times
		
		
	end
end)


-- save when player is offline
game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Tokens"] = Player.leaderstats.Tokens.Value; -- Change "Money" with your currency.
		["Kills"] = Player.leaderstats.Kills.Value;
	  ["Times"] = Player.leaderstats.Times.Value;})
end)





 -- Save Kills
local Players = game.Players

local Template = Instance.new 'BoolValue'
Players.PlayerAdded:connect(function(Player)
	local Stats = Template:Clone()
	Stats.Parent = Player
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return 
					end
				end
			end)
		end
	end)
end)

Here!

-- Variable
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") 

-- Create folders in Local Player

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Tkn= Instance.new("IntValue", Leaderstats)
	Tkn.Name = "Tokens" 
	Tkn.Value = 0

	local Kills = Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
	
	local Times = Instance.new("IntValue", Leaderstats)
	Times.Name = "Time"
	Times.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Tkn.Value = Data.Tokens 
		Kills.Value = Data.Kills 
		Times.Value = Data.Times
		
		
	end
end)


-- save when player is offline
game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Tokens"] = Player.leaderstats.Tokens.Value; -- Change "Money" with your currency.
		["Kills"] = Player.leaderstats.Kills.Value;
	  ["Times"] = Player.leaderstats.Times.Value;})
end)





 -- Save Kills
local Players = game.Players

local Template = Instance.new 'BoolValue'
Players.PlayerAdded:connect(function(Player)
	local Stats = Template:Clone()
	Stats.Parent = Player
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return 
					end
				end
			end)
		end
	end)
end)

This method I’m replying to should work. If it is not working the reason is one of the following:

OnServerEvent must be used in a server-side script (I recommend making it its own script to prevent further complication just in-case).

FireServer must be used in a client script aiming at the button.

For the sake of trouble shooting the data store, I must ask, does the data store save your stats when you do not reset your stats?

I have the same things to say as the reply above this one.