Help with Script

Hello Developers,
My question is if there is a way to see if a Folder of a Player has a specific accessory. Im making a Halo System at the Moment, if you own a Halo it gets added to a folder in the Player called “Owned”. I also made a shop for this. If u click a specific Halo (for example Red Halo) a Text of a BUY button should change to EQUIP if the Player has the Halo in the Folder, else it says BUY. Basically I want to know how to make a simple check function if the Player has the specific accessory in the Folder.
Help would be appreciated! :grin:

You can check if an instance exists like this:

local folder = -- your folder
local item = folder:FindFirstChild("your item") -- this will return the item if it exists, or return nil if it doesn't

if item then -- check if item is NOT nil
--change the text to EQUIP
else
--change the text to BUY
end

Run this every time they open the shop or buy something

Thats all what I’ve wanted to know! 1 last question, Im also making the Equip function so that if you equip a Halo all other Halos get destroyed. The Halos are all in the “Owned” Folder, is there a way to make a script which checks if 1 of these Halos is equipped by the Player (if yes these Halos should get Removed off the Players Character) Thank you so much for your fast Reply! Marked it as the Solution!

This script should destroy all items, which is duplicated.
I hope this will help.

local Folder = workspace.Folder -- your Folder with duplicated items
local Owned = workspace.Owned -- your Folder with owned items 

function CheckItem()
	local item = Owned:FindFirstChild("Part") -- your accessory/item
	local itemNotToDestroy = Folder:FindFirstChild(item.Name)
	if itemNotToDestroy then
		for i, v in pairs(Folder:GetChildren())do
			if v ~= itemNotToDestroy then
				v:Destroy()
			end
		end
	end
end

CheckItem()
2 Likes

Thanks for your Reply! The only problem here is that I have multiple accessories (1 of them could be in the Players Character… I could possible check it for EVERY single halo, but isn’t there a cleaner way to do this?)

You can check if a specific Halo exists in the folder the same way as before, or use :FindFirstChildWhichIsA() (here), or use a for loop and go through all children of the folder

local folder = --folder

for i, v in pairs(folder:GetChildren()) do --gets every children of the folder, you can also use :GetDescendants() if there are children of the children of the folder
     if v:IsA("Accessory") then
          if string.match(v.Name.lower, "halo") then
               v:Destroy() -- this destroys everything that has the "Halo" word in its name and is an accessory
          end
     end
end
2 Likes

Oh my god I did not even know this function even exists! Thank you so much!!!

1 Like

Ok 1 LAST question. Im also making an unequal function. The Text of a Buy Button should be “UNEQUIP” if the Player OWNS the Halo (so the Halo is in the Folder), BUT not has it equipped in its character. For this the Script should check if the Player has currently equipped the Halo and owns it.
I made some code:


for i, v in pairs(OwnedFolder:GetChildren()) do 
	for _,accessory in pairs(game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):GetAccessories()) do
	if v:IsA("Accessory") then
		if v.Name == HaloName and accessory then
			Buy.Buy3.Text = "UNEQUIP" -- the text of the buy button
			end
		end
	end
end

the only problem is I have to check the Same function if the Halo is owned but NOT equipped, so the text should be “EQUIP”, and also if the Player doesn’t even own the Halo the Text should be “BUY”…
For this I have to use else if function and check functions if the Player owns the Halos or not,etc…
Is there a way to clean this up and not use the for I,v in pairs(()) do function? Because if I have to use this I can’t use else if functions :confused: (sorry im not experienced at scripting)

if game.Players.LocalPlayer.Character:FindFirstChild(HaloName) then

would this be an option?

I dont think theres an other way, but you can use else if, you just have to make a local accExists = false bool outside of the second loop, and set it to true if the accessory is equipped in the second loop. Then you can say if accExists == false then ...

1 Like

but would this work too? (character limit)

Yes, if you know the HaloName already

1 Like

I just named the Halo Button same as the Halo which you can buy there, so The Halo Name is there :slight_smile:

1 Like

Hey so I just tested out this function (sorry for the late reply) and I got this error in output:
ServerScriptService.HaloScripts.HaloHandler:35: invalid argument #1 to ‘match’ (string expected, got function)

I changed the Script to this:

for i, v in pairs(Folder:GetChildren()) do 
		if v:IsA("Accessory") then
			if string.match(v.Name.lower, "Halo") then
				v:Destroy() 
			end
		end
	end

Make sure the “Halo” part is in full lowercase so “halo”, or else it won’t work (because .lower makes everything lowercase too)

1 Like

is there a way to check for the halos which the player could wear (i just want that every possibly equipped halo gets deleted when equipping another halo), I have a folder called “OwnedHalos” in the Player, where all owned Halos are stored as accessories…

maybe there is a way if the Player has an accessory and it is called same as 1 of these halos then it gets destroyed?

The Problem here is if the Player wears a Roblox accessory which has the strings “halo” it gets deleted too :confused:

Loop through all the halo accessories and see if the folder contains it, and if it does delete it. Use folder:FindFirstChild(halo.Name) and then halo:Destroy()

for _, halo in pairs(otherFolder:GetChildren()) do -- otherFolder is the one where all the halos are stored
	if halo:IsA("Accessory") then
		if folder:FindFirstChild(halo.Name) then
			halo:Destroy() 
		end
	end
end

Something like this

1 Like

Sorry for the late Reply. I will try this out later. Thank you so much!!!