Need help with datastore

Hello! I dont have much coding experience, and i am making an obby. I am trying to make a “clear progress” button, and i need help with this. Here is what i have so far for removing the async. It doesnt work and i dont know why. I know it might see dumb to all the programmers out there, but reminder, i have barely any experience with lua. :sweat_smile:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Stage")

script.Parent.MouseButton1Click:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")

	local stageKey = player.UserId.."-Stage"
	
	local success, error = pcall(function()
		dataStore:RemoveAsync(stageKey)
	end)

	if not success then
		warn("Failed to wipe data: " .. error)
	end
end)

Your script is almost correct, but there are a couple of issues:

  • Accessing the Player: The MouseButton1Click event is on a GUI button, so the player argument won’t automatically be passed. You need to correctly reference the player interacting with the button.

Here’s a fixed and improved version of your script:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Stage")

-- Reference the LocalPlayer through the Player GUI
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	-- Ensure the player and their leaderstats exist
	if player and player:FindFirstChild("leaderstats") then
		local stageKey = player.UserId.."-Stage"
		
		-- Try to remove the player's data from the DataStore
		local success, error = pcall(function()
			dataStore:RemoveAsync(stageKey)
		end)

		if success then
			-- Reset the stage value in leaderstats
			player.leaderstats.Stage.Value = 0
		else
			warn("Failed to wipe data: " .. error)
		end
	else
		warn("Player or leaderstats not found.")
	end
end)
1 Like

Is this a server Script or a LocalScript?

Thanks! Like i said, i dont have much coding experience, so your help is much appreciated!

Well, let me tell you one thing: you’re very welcome.
Additionally, just a note from me—I’m available for hire (for free, just to pass the time and have fun).

1 Like

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