How can i make a button that will delete the player progress

i just made a datastores for my game yesterday and wanted to know how can i make a “new game” button so bassicly once the player beats the game it will delete there datastores and they can play the game again

if it deleted there datastore then when they play the game again will it not save there progress?

datastore script

-- local DataSoreService = game:GetService("DataStoreService")

local myDataStore = DataSoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local collectibles = Instance.new("IntValue")
	collectibles.Name = "collectibles"
	collectibles.Parent = leaderstats
	
	local data
	local succes, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-collectibles")
	end)
	
	if succes then
		collectibles.Value = data
	else
		print("there was an error")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	pcall(function()
		myDataStore:SetAsync(player.UserId.."-collectibles",player.leaderstats.collectibles.Value)
	end)
	
	if succes then print("data saved")
	else
		print("there was an error")
		warn(errormessage)
	end
	
end)

1 Like

Simply create a remote event in ReplicatedStorage and name it “ResetStage,” then add this to your local script:

game.ReplicatedStorage.ResetStage:FireServer(clicked)

Then, write a script, put it in ServerScriptService, and include the following:

game.ReplicatedStorage.ResetStage.OnServerEvent:Connect(function(clicked)
    clicked.leaderstats.Stage.Value = 1
    clicked.leaderstats.Attempts.Value = 0 
    clicked.Character.Humanoid.Health = 0
end)
3 Likes

hmmm there are multiple worlds in the game that are in different “games” is there a way to kick the player and set there collectible value to 0?

Just like @0hJudas said,
just changing the value of the player’s collectibles on the serverside to 0 would automatically save this value whenever the player leaves the game, so you don’t really have to tamper with the datastore itself

soo, you now just have to change the player’s collectibles value to 0 and then kick them, and their datastore values will become as good as new

let me give more context
i am making a 3d plat former game kinda like mario odessey and the way im separating the different worlds is im using different “places” like games and there are teleporters in each world to teleport (the way you open the portal is to get 5 collectibles or “stars”) you to the next so bassicly each world is a whole new game

the last world doest have any datastores since its just a one time bossfight once you beat the boss the ui to restart the game will apear how would i make the button kick the player and also delete the datastores in the other worlds

Hmmmmm…

If am getting you right, you’re using multiple places under the same game, soo you want to reset the values after the player finishes the game at the final place.
So now am assuming after the player finishes the boss level you’ll be teleporting them to the starter place, so now you have to change the player’s value to 0 and the use the set async function separately before teleporting them.

pcall(function()
		myDataStore:SetAsync(player.UserId.."-collectibles",player.leaderstats.collectibles.Value)
	end)
	
	if succes then print("data saved")
	else
		print("there was an error")
		warn(errormessage)
	end

You probably would have to run this under serverscriptservice through a remote event after the particular player completes the game

2 Likes

yes that is what im talking about just not sure how to do it since it would have to reset data stores and kick the player

local function OnRemoteFired(Player)
	local Success, Result = pcall(function() return DataStore:RemoveAsync(Player.UserId) end) --Wipe data.
	if not Success then warn(Result) end --Warn if necessary.
	Player:Kick() --Kick player.
end

You’d just connect a function akin to the above to a RemoteEvent’s ‘OnServerEvent’ signal (on the server of course).

1 Like

i just found out that the data stores save from all the different places which is good if i want to delete them but its not good since if you need 5 collectibles to beat the first world then once you get to the second then you would already have beat it.

would i have to make a new datastore?