Trying to unequip a tool before data saves

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to try and unequip all the player’s tools even the one the player has equipped once the player’s leave ( to be exact i want to unequip the tools before i save something in the tools with data stores )

  2. What is the issue? Include screenshots / videos if possible!
    the issue is i only got it to work if the player doesn’t have any tools equipped if the player does have a tool equipped and leaves the game then none of the data of that tool would save.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried to use “:UnequipTools()” but I’m using playeradded and playerremove events so that doesn’t work since the character has already been removed. I’ve been trying to figure out a way to use CharacterRemoving but it keeps activating after the data save (which doesn’t save the tool the player has equipped).

local function save(player)	
	if not RunService:IsStudio() then --- set to not save in studio
		return
	end
	print("SavingData")
	local colorsaved = colortable(player)
	local success, errormessage = pcall(function()
		local PlayerUserId = 'Player_'.. player.UserId
		print("DataSaved")
		ColorStorage:SetAsync(PlayerUserId, colorsaved)
	end)
end

Players.PlayerAdded:Connect(function(player)
	load(player)
end)

game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		save(player)
	end
end)

I’ve also found this piece of code on the internet somewhere, i don’t really know how to add it to main script since this would activate after the data saved

    Player.CharacterRemoving:Connect(function(Character)
        Character:WaitForChild("Humanoid"):UnequipTools()
        print("ToolsUnequiped")
    end)
1 Like

I don’t understand, why would you need to unequip the tools once a player leaves?

i need to save a a part’s color in a tool but it doesn’t really work if they have equipped, so i tried to make it so it unequips all the tools then it’ll save their colors

You could maybe store all the tool clones in 1 folder inside the Player, and reference this folder instead.

1 Like

yeah ig i could do this, i’ll try this out then i’ll mark you as solution

1 Like
local function save(player)	
	if not RunService:IsStudio() then --- set to not for studio testing
		return
	end
	print("SavingData")
	local colorsaved = colortable(player)
	local success, errormessage = pcall(function()
		local PlayerUserId = 'Player_'.. player.UserId
		print("DataSaved")
		ColorStorage:SetAsync(PlayerUserId, colorsaved)
	end)
end

Players.PlayerAdded:Connect(function(player)
	load(player)
	player.CharacterAdded:Connect(function(character)
		local hum = character:WaitForChild("Humanoid")
		hum:EquipTools()
	end)
	player.CharacterRemoving:Connect(function(character)
		local hum = character:WaitForChild("Humanoid")
		hum:UnequipTools()
		save(player)
	end)
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		save(player)
	end
end)

CharacterRemoving is fired before the player leaves and also every time they die, it’s a good idea to unequip the tools then anyway, to prevent players from shooting when dead.

3 Likes