Hey Devs,
so I have a Folder called “OwnedHalos” in the Player. Im currently making a Halo shop with Equip and Unequip functions. When I bought a Halo in the Shop the bought Halo gets cloned into the OwnedHalos Folder. I want a function that when you equip a Halo the Script firstly checks if the Player is currently equipping 1 of the Halos in the owned Halos Folder. For example you have 2 bought Halos (red halo and blue halo). You are equipping the Red Halo and after that you somehow want to equip the Blue Halo. You click the Blue Halo Button and Equip it. Now you have 2 Halos on you players character. The Script should’ve checked here if the Player equipped 1 of the Halos in the Folder (there are multiple Halos, not only this 2).
So my question:
Is there a way to make a Script which checks if the Player has currently equipped 1 accessory which is also stored in a Folder? If yes, the accessory should get destroyed and after that the new one should be equipped. Here is my current Code for the Script which equips the Halo (works fine)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipEvent = ReplicatedStorage.HaloEvents.EquipHalo
local Halo = ReplicatedStorage.HaloEvents.Halos
EquipEvent.OnServerEvent:Connect(function(player)
local Folder = player:WaitForChild("OwnedHalos")
local character = player.Character
local humanoid = character.Humanoid
local HaloName = player:WaitForChild("HaloStuff").HaloName.Value
local clone = Folder[HaloName]:Clone()
humanoid:AddAccessory(clone)
end)
The HaloStuff.HaloName function just gets the Name of the current clicked Halo Button (complicated).
Here is the Code I tried to add in the Script (same script just with the not working function):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipEvent = ReplicatedStorage.HaloEvents.EquipHalo
local Halo = ReplicatedStorage.HaloEvents.Halos
EquipEvent.OnServerEvent:Connect(function(player)
local Folder = player:WaitForChild("OwnedHalos")
local character = player.Character
local humanoid = character.Humanoid
local HaloName = player:WaitForChild("HaloStuff").HaloName.Value
local clone = Folder[HaloName]:Clone()
for _, halo in pairs(player.Character:WaitForChild("Humanoid"):GetAccessories()) do
if halo:IsA("Accessory") and halo.Name ~= game.ReplicatedStorage.HaloEvents.Halos:FindFirstChild(halo.Name) then
halo:Destroy()
end
end
wait(0.5)
humanoid:AddAccessory(clone)
end)
this script work too BUT it destroys EVERY SINGLE accessory the player has equipped right now.
Any help would be appreciated!