Reset datastore gui button doesnt seem to be working (datastore 2 electric boogaloo)

Morning dev forum peeps, this question is very muich similar to my post yesterday which was:

And yes my question was answered but it seems like my value data is still saving when i press the gui button and get kicked and i have no idea why.

Heres what my script looks like currently, (take note of the section where its supposed to remove async)

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local ds = DataStoreService:GetDataStore("theboy1234")
local ResetData = ReplicatedStorage.ResetData

--//Tables
local questions = {
	"Where are we?",
	"Why am i here?",
	"something even sillier",
	"ghost",
	"Tool",
	"Tool2"
}

local actions = {
	"pressed prompt",
	"pressed prompt2",
	"pressed button",
	"has tool",
	"has badge",
	"has tool2",
	"got the time!"
}

local pressedstuff = {
	"pressed this"
}

--//Functions
local function SaveData(player, data)
	local success, errorMessage = pcall(function()
		ds:SetAsync(player.UserId, data)
	end)

	if not success then
		warn(errorMessage)
	end
end

local function OnPlayerRemoving(player)
	local datasave = {}

	for n, v in player.Booleans:GetChildren() do
		table.insert(datasave, {name = v.Name, Booleans = {}})

		for i, v in v:GetChildren() do
			table.insert(datasave[n].Booleans, {name = v.Name, value = v.Value})
		end
	end

	SaveData(player, datasave)

	print(datasave)
end

ResetData.OnServerEvent:Connect(function(player)
	player:Kick("Your data has been reset.")
	ds:RemoveAsync(player.UserId)
end)

Players.PlayerAdded:Connect(function(Player)
	local booleans = Instance.new('Folder')
	booleans.Name = 'Booleans'
	booleans.Parent = Player

	local Answered = Instance.new("Folder")
	Answered.Name = "Answered"
	Answered.Parent = booleans

	local Actions = Instance.new("Folder")
	Actions.Name = "Actions"
	Actions.Parent = booleans

	local PressedStuffs = Instance.new("Folder")
	PressedStuffs.Name = "PressedStuffs"
	PressedStuffs.Parent = booleans

	for i, PresseStuff in pressedstuff do
		local booleanvalue = Instance.new("BoolValue")
		booleanvalue.Name =  PresseStuff
		booleanvalue.Parent = PressedStuffs
	end

	for i, Action in actions do
		local booleanvalue = Instance.new("BoolValue")
		booleanvalue.Name = Action
		booleanvalue.Parent = Actions
	end

	for i, Question in questions do
		local booleanvalue = Instance.new("BoolValue")
		booleanvalue.Name = Question
		booleanvalue.Parent = Answered
	end

	local success, dataofuser = pcall(function()
		return ds:GetAsync(Player.UserId)
	end)

	if dataofuser then -- anything but nil		
		print(dataofuser)

		for i, k in dataofuser do
			for i, v in k.Booleans do
				booleans[k.name][v.name].Value = v.value
			end
		end
	else
		print(success, dataofuser)
	end
end)

Players.PlayerRemoving:Connect(OnPlayerRemoving)

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

So yea thas pr much it, i dont have anything to build off of because theres nothin in output and it isnt putting me into debug mode so im in the dark. Help would be appreciated and thanks in advanced

Well, if you kick the player and remove their data, wouldn’t it just run the playerremoving code and save their data again? I would assume you need some code to check if the player is being kicked before saving their data in that function

Thats what i was wondering too when looking at the script, i tried putting removeasync before the kick and then using wait() before it kicks you but that didnt seem to work.

and yeah your correct cause it seems to still save the table

You can set an attribute on the player before they get kicked and check for it in the playerremoving function, if it exists then just return without saving

Or make a table which adds their name to it when they get kicked, check for their name in player removing, if it exists remove it and return

I see, sorry for asking but which of the two would be more like “optimal” in a way

Just using attributes makes more sense to me

ResetData.OnServerEvent:Connect(function(player)
	ds:RemoveAsync(player.UserId)
        player:SetAttribute("Removing", true)
	player:Kick("Your data has been reset.")
end)
local function OnPlayerRemoving(player)
        if player:GetAttribute("Removing") then return end
	local datasave = {}

	for n, v in player.Booleans:GetChildren() do
		table.insert(datasave, {name = v.Name, Booleans = {}})

		for i, v in v:GetChildren() do
			table.insert(datasave[n].Booleans, {name = v.Name, value = v.Value})
		end
	end

	SaveData(player, datasave)

	print(datasave)
end
1 Like

ah oke, i presume reset data replaces the old reset data, but where am i putting local function onplayerremoving? nvm i figured it out thanks a million dude

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