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)
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!