How to make button that will reset all stats? (Datastore 2)

so i made a gui with a reset button, and i want to make that if player has right amount of gold and level then everytime player presses this button it will set their GOLD,LOVE,EXP to 0, and add 1 Reset

here’s my datastore2 script:

--variables
local DataStore2 = require(game:GetService("ServerScriptService").MainModule)
local mainkey = "as"
DataStore2.Combine(mainkey, "Stats","SOULS","ITEMS","FOOD","ARMOR")

-- data table
local function SetDataTable()
	local UserData = {
		Stats = {
			["EXP"] = 0,
			["GOLD"] = 0,
			["RESET"] = 0,
			["LOVE"] = 1
			},
		SOULS = {
			["BasicSoul"] = true,
			["Determination"] = false,
			["Integrity"] = false,
			["Justice"] = false,
			["Perseverance"] = false,
			["Kindness"] = false,
			["Bravery"] = false,
			["Patience"] = false
		},
		ITEMS = {
			["Stick"] = true,
			["REALKnife"] = false	
		},
		ARMOR = {
			["Bandage"] = true,
			["HeartLocket"] = false	
		},
		FOOD = {
			["MonsterCandy"] = 0,	
		},
	}
	return UserData
end

--main

game.Players.PlayerAdded:Connect(function(plr)
	local UserData = DataStore2(mainkey,plr):Get(SetDataTable())
	
	local gaster = Instance.new("Folder")
	gaster.Name = "TobyFox"
	local soulsfolder = Instance.new("Folder")
	soulsfolder.Name = "SOULS"
	local ITEMS = Instance.new("Folder")
	ITEMS.Name = "ITEMS"
	local ARMOR = Instance.new("Folder")
	ARMOR.Name = "ARMOR"
	local FOOD = Instance.new("Folder")
	FOOD.Name = "FOOD"
	
	--userStats
	local LOVE = Instance.new("IntValue")
	LOVE.Name = "LOVE"
	local GOLD = Instance.new("IntValue")
	GOLD.Name = "GOLD"
	local RESET = Instance.new("IntValue")
	RESET.Name = "RESET"
	local EXP = Instance.new("IntValue")
	EXP.Name = "EXP"
	
	--souls
	local BasicSoul = Instance.new("BoolValue")
	BasicSoul.Name = "BasicSoul"
	local Determination = Instance.new("BoolValue")
	Determination.Name = "Determination"
	local Justice = Instance.new("BoolValue")
	Justice.Name = "Justice"
	local Integrity = Instance.new("BoolValue")
	Integrity.Name = "Integrity"
	local Bravery = Instance.new("BoolValue")
	Bravery.Name = "Bravery"
	local Kindness = Instance.new("BoolValue")
	Kindness.Name = "Kindness"
	local Patience = Instance.new("BoolValue")
	Patience.Name = "Patience"
	local Perseverance = Instance.new("BoolValue")
	Perseverance.Name = "Perseverance"
	
	--items
	local Stick = Instance.new("BoolValue")
	Stick.Name = "Stick"
	local REALKnife = Instance.new("BoolValue")
	REALKnife.Name = "REALKnife"
	
	--Armor
	local Bandage = Instance.new("BoolValue")
	Bandage.Name = "Bandage"
	local HeartLocket = Instance.new("BoolValue")
	HeartLocket.Name = "HeartLocket"
	
	
	--Food
	local MonsterCandy = Instance.new("IntValue")
	MonsterCandy.Name = "MonsterCandy"
	
	--StoreInFolder
	StatsData = DataStore2("Stats", plr)
	local SoulsData = DataStore2("SOULS", plr)
	local ItemsData = DataStore2("ITEMS",plr)
	local FoodData = DataStore2("FOOD", plr)
	local ArmorData = DataStore2("ARMOR", plr)
	
	--Stats data
	local function UpdateStats(UpdateValue) -- updates value to current data
		LOVE.Value = StatsData:Get(UpdateValue).LOVE
		EXP.Value = StatsData:Get(UpdateValue).EXP
		GOLD.Value = StatsData:Get(UpdateValue).GOLD
		RESET.Value = StatsData:Get(UpdateValue).RESET
	end
	
	--Soul data
	local function UpdateSouls(UpdateValue)
		BasicSoul.Value = SoulsData:Get(UpdateValue).BasicSoul
		Determination.Value = SoulsData:Get(UpdateValue).Determination
		Perseverance.Value = SoulsData:Get(UpdateValue).Perseverance
		Patience.Value = SoulsData:Get(UpdateValue).Patience
		Kindness.Value = SoulsData:Get(UpdateValue).Kindness
		Bravery.Value = SoulsData:Get(UpdateValue).Bravery
		Justice.Value = SoulsData:Get(UpdateValue).Justice
		Integrity.Value = SoulsData:Get(UpdateValue).Integrity
	end
	
	--update data
	UpdateStats(UserData.Stats)
	UpdateSouls(UserData.SOULS)
	
	StatsData:OnUpdate(UpdateStats)
	SoulsData:OnUpdate(UpdateSouls)
	
	--parent
	gaster.Parent = plr
	soulsfolder.Parent = plr
	
	--allinonestats
	--stats
	LOVE.Parent = gaster
	RESET.Parent = gaster
	EXP.Parent = gaster
	GOLD.Parent = gaster
	--Souls
	Determination.Parent = soulsfolder
	BasicSoul.Parent = soulsfolder
	Perseverance.Parent = soulsfolder
	Integrity.Parent = soulsfolder
	Justice.Parent = soulsfolder
	Kindness.Parent = soulsfolder
	Bravery.Parent = soulsfolder
	Patience.Parent = soulsfolder
		--LEVE SYSTEN start
EXP.Changed:connect(function()
	if UserData.Stats.LOVE < 100 then
		if UserData.Stats.EXP >= UserData.Stats.LOVE*25 + 10 then
			UserData.Stats.EXP = UserData.Stats.EXP - (UserData.Stats.LOVE*25 + 10)
			UserData.Stats.LOVE = UserData.Stats.LOVE + 1
			StatsData:Set(UserData.Stats)
		end
	end
end)
LOVE.Changed:connect(function()
	if UserData.Stats.LOVE >= 100 then
		UserData.Stats.LOVE = 100
		StatsData:Set(UserData.Stats)
	end
end)
end)
workspace.yesname.ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
	local UserData = DataStore2(mainkey,PlayerWhoClicked):Get(SetDataTable())
	local StatsData = DataStore2("Stats", PlayerWhoClicked)
	local EXP = PlayerWhoClicked.TobyFox.EXP
	local LOVE = PlayerWhoClicked.TobyFox.LOVE
	if UserData.Stats.LOVE < 100 then
	 UserData.Stats.EXP = UserData.Stats.EXP + 2000
	StatsData:Set(UserData.Stats)
	if UserData.Stats.EXP >= UserData.Stats.LOVE*25 + 10 then
			UserData.Stats.EXP = UserData.Stats.EXP - (UserData.Stats.LOVE*25 + 10)
			UserData.Stats.LOVE = UserData.Stats.LOVE + 1
			StatsData:Set(UserData.Stats)
	end
	end
end)
workspace.goldname.ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
	local UserData = DataStore2(mainkey,PlayerWhoClicked):Get(SetDataTable())
	local StatsData = DataStore2("Stats", PlayerWhoClicked)
	UserData.Stats.GOLD = UserData.Stats.GOLD + 100000
	StatsData:Set(UserData.Stats)
end)
1 Like

Good Question, I asked @Kampfkarren this and he said that :Set(nil) should work.

but how i can get the player , because if you go from game.players.localplayer.Stats.GOLD.Value = 0, then it will do nothing , it has to be Userdata.Stats.GOLD = 0, but i can’t get UserData from other script, and even if i do it in the same script, i has to know the player who clicked, but you can’t get the player who clicked from Activated function, so what am i doing?

Correct me if I am wrong but, if you had a button that reset data when the player clicks it and you fire an event to the server which handles the reset. The event fired will have the player, so for example -

resetData.OnServerEvent:connect(function(plr)
	local userDatastore = DataStore2("DataStore", plr)
	userDatastore:Set(nil)
end)
3 Likes

wait, how i do that, i have now a TextButton that does nothing, so do i have to add to replicatestorage a Remote event , and to the TextButton a localscript with a code in it that will fire the name of the player to the remote event?

button = script.Parent
button.Activated:Connect(function()
game.ReplicatedStorage.Playername:FireServer
end)

something like this? i’m not a good scripter so idk

1 Like

You could probably do it that way.

button = script.Parent
button.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.ResetData:FireServer()
    --- ResetData is the name of the remote
end)

Remote events/functions automatically receive the player who fired it as their first argument.

1 Like

and what i do after that, how do i take the player name from serverscriptservice script?

Using @SkoobiDoobiDoo’s code, you receive the player object as the first argument of the remote’s event, so to access any property (including name), you literally just need to write plr.Name.

3 Likes

yay it worked, thank you both guys @SkoobiDoobiDoo and @ClockworkSquirrel , i can’t give solution to both of you so idk what to do xD, any suggestion?

just mark your own if possible lol