How would I make a equip and unequip system? (For Ingame Accessories)

Hello!
I currently joined the devforum and I would like some help for my code to get improved.

I currently made a TextButton that makes a player unlock a certain hat when reaching a certain level on my game.

It works well when equipping and unequipping the hat. But whenever the character resets or respawns I cant equip the hat anymore.

Details

  • When the player character dies. It doesn’t save the hat that is equipped. What I mean is saved is when the character equipped the hat and then suddenly dies. It doesn’t come back.
  • As the issue above, when the character dies it doesnt let the player equip it anymore. But the text changes to equip and unequip.

Here is a example of my code.



script.Parent.MouseButton1Click:Connect(function()
	if Player.leaderstats.Level.Value >= 10 and equippedhalo == false then
		equippedhalo = true
		givehat = hat:Clone()
		givehat.Parent = character
		wait()
		script.Parent.Text = "Equipped"
	elseif Player.leaderstats.Level.Value >= 10 and equippedhalo == true then
		equippedhalo = false
		character:FindFirstChild("TestHalo"):Destroy()
		script.Parent.Text = "Equip"
	elseif Player.leaderstats.Level.Value <= 10 then
		script.Parent.Text = "Locked"
	end
end)

If you do help me out of this. I would like to thank you very much :smile:

If you want to explain furthermore detail’s about the coding.
You can dm me on discord. Classyblade#9882

Thank you very much!

1 Like

There is an API for this. HERE
Also this should be in Scripting Support.

2 Likes

Ohh okay! Thank you very much.

I will study the API.

This is for tools, not accessories lol

@996_7 use Humanoid:AddAccessory() instead

1 Like

You should clean up your code a bit

script.Parent.MouseButton1Click:Connect(function()
	if Player.leaderstats.Level.Value >= 10 then -- Check the level alone first
		if equippedhalo then -- Then check if it's equipped
			givehat = hat:Clone()
			givehat.Parent = character
			wait()
			script.Parent.Text = "Equipped"
		else
			character:FindFirstChild("TestHalo"):Destroy()
			script.Parent.Text = "Equip"
		end

		equippedhalo = not equippedhalo -- Then invert the boolean variable (If it's [true] it will become [not true] aka [false] and vice versa)
	else -- if level is not greater than or equal to 10 then do this:
		script.Parent.Text = "Locked"
	end
end)

as for the problem of why it doesn’t work after the character dies, I will need more info on that because with the info you gave I have no clue. Is it a script or a local script? What is script.Parent?

1 Like

Oh. It is a script, and the script.Parent refers to the TextButton that make’s the player equip the hat.

Just woke up sorry for the late reply.

When I implemented your code it had a error. Tried to fix it but I didnt really fix anything since I’m new to this.

I should send the full code so that you can understand the script.

local Player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local hat = game.ServerStorage.Halos:WaitForChild("TestHalo")
local character = Player.Character

local equippedhalo = script.Parent.Equipped.Value
equippedhalo = false

script.Parent.MouseButton1Click:Connect(function()
	if Player.leaderstats.Level.Value >= 10 and equippedhalo == false then
		equippedhalo = true
		givehat = hat:Clone()
		givehat.Parent = character
		wait()
		script.Parent.Text = "Equipped"
	elseif Player.leaderstats.Level.Value >= 10 and equippedhalo == true then
		equippedhalo = false
		character:FindFirstChild("TestHalo"):Destroy()
		script.Parent.Text = "Equip"
	elseif Player.leaderstats.Level.Value <= 10 then
		script.Parent.Text = "Locked"
	end
end)

This is my full code.

My answer’s for your questions.

  • script.Parent refers to the TextButton that the script is in.
  • This is a Script and not a LocalScript.

I have finished studying about the auto-equip and mostly the hat data store for the player.

Thanks for the people that tried to help me! :grinning_face_with_smiling_eyes: