Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is :Destroy() not working on an Accessory?

Hello everyone! I feel like I am missing something very obvious. Here is the problem:

I have an Accessory located in the player's character. I am attempting to make a GUI that will "unequip" this hat, by simply destroying it.

Here is the code:

if script.Parent.Text == "Unequip" and check == true then
		local hat = char:FindFirstChild(currentHat.Name)
		if hat then
			print(hat.Name)
			hat:Destroy()
			script.Parent.Text = "Equip"
			script.Parent.BackgroundColor3 = Color3.new(0, 211, 0)
		end
	end

The code gives no errors, and it prints the correct hat.Name. This is also written in a normal script, not a local script. Any ideas on why it is not working?

2 answers

hat is defined as the hats name, possibly? so make variable

local hat = char:FindFirstChild(currentHat).

I don't think you need to put the .Name ?

This script should work, the problem is since you're just using an if statement and not a listening event it needs to constantly run to check the value

while true do
	wait()
	if script.Parent.Text == "Unequip" and check == true then
	        local hat = char:FindFirstChild(currentHat.Name)
	        if hat then
	            print(hat.Name)
	            hat:Destroy()
	            script.Parent.Text = "Equip"
	            script.Parent.BackgroundColor3 = Color3.new(0, 211, 0)
		end
	end
end

WARNING: Using many while true do loops in succession can cause server lag, I would strongly advise you to switch this from a Script to a Local Script. Local Scripts have GUI event listeners and are strongly advised. You can use the .Activated event in succession with a remote event to solve this. You will need multiple scripts, firstly a Local Script which is a child of the GUI button:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("removeHat")

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local char = Player.Character
local button = script.Parent

local function click()
	if button.Text == "Unequip" then
	        local hat = char:FindFirstChild(currentHat.Name) --Dont forget to add current var
	        if hat then
	            print(hat.Name)
	            removeHat:FireServer(hat)
	            script.Parent.Text = "Equip"
	            script.Parent.BackgroundColor3 = Color3.new(0, 211, 0)
		end
	end
end
button.Activated:Connect(click)

In ReplicatedStorage add a RemoteEvent and name it removeHat

Finally, put this in a normal Script object in the ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("removeHat")

remoteEvent.OnServerEvent:Connect(function(hat)
	hat:Destroy()
end)

I really hope this helped, if it did you can mark it as the answer. Also don't forget to comment if there's any errors, good luck!

This discussion is preserved as it was. New answers, comments, and votes are closed.