Proper item checker script help

Hello, I’ve decided to give up trying to fix this issue because I haven’t found how to do it by myself and it has made me quite frustrated, could anyone help me make this code run, check if the player has said cosmetic on themselves and if not then give them the item, but if they do then do nothing.

the issue at the moment is that i don’t know how to make it so it only runs once rather than per each item in the player.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local clickedEvent = ReplicatedStorage:WaitForChild('PillarClicked')
local childChangedEvent = ReplicatedStorage:WaitForChild('childChangedEvent')

local function onClick(player, character, headFolder)
	
	for _, child in (headFolder:GetChildren()) do
		local split = child.Name:split("_")
		if child:IsA('Model') or child:IsA('Accessory') then
			for _, cosmetic in character:GetDescendants() do
				if cosmetic:IsA('Model') or cosmetic:IsA('Accessory') then
					if cosmetic.Name == child.Name then
						print(child.Name, 'found')
					else
						print(child.Name, 'not found')
						local folder = character:FindFirstChild('Unusual_Inventory')
						local clone = child:Clone()
						local holder = clone:WaitForChild('plh_parent')

						childChangedEvent:FireClient(player, child)
						clone.Parent = folder
						holder.Anchored = true
						break
					end
				end
			end
		end
	end
end

clickedEvent.OnServerEvent:Connect(onClick)
3 Likes

well if anybody would like to help me it would be nice, its late so i might not reply for a while.

2 Likes

local tooltofind = “Tool”

script.Parent.Touched:Connect(function(hit)

local humanoid = hit.Parent:FindFirstChild("Humanoid")
local player = game.Players:FindFirstChild(hit.Parent.Name)

if player ~= nil and humanoid ~= nil then
	
	local playertool = player.Backpack:FindFirstChild(tooltofind)
	local charactertool = humanoid.Parent:FindFirstChild(tooltofind)
	if charactertool ~= nil or playertool ~= nil then
		print("found tool in player")
		
	end
	
end

end)

2 Likes

In your script, you have done

for _, child in (headFolder:GetChildren()) do

which isn’t supposed to be I guess. Do

for _, child in pairs(headFolder:GetChildren()) do
2 Likes

you can use either top one is just faster to type.

2 Likes

I don’t know what this is for…, its a completely different script without any context attached.

1 Like

when a player touches a part it checks if it is a player and finds if the player has the tool (i only read the title)

2 Likes

The better method to get the player from character is

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
1 Like

Dude pairs is neccessary, as it pairs the variable child with the children

pairs() or not the issue isn’t really there its everything else after it

1 Like

pairs() is used with dictionaries so i dont see how this would work for something like a part or a workspace object.

local tooltofind = “Tool”–tools name

script.Parent.Touched:Connect(function(hit)–when something touches the part

local humanoid = hit.Parent:FindFirstChild("Humanoid")--finds humanoid in hits parent
local player = game.Players:FindFirstChild(hit.Parent.Name)--finding hits parent name(characters name) in players

if player ~= nil and humanoid ~= nil then--if the humanoid isnt nil and the player isnt nol too
	
	local playertool = player.Backpack:FindFirstChild(tooltofind)--finds tool in players backpack
	local charactertool = humanoid.Parent:FindFirstChild(tooltofind)--finds tool in character
	if charactertool ~= nil or playertool ~= nil then--if the tool was found in players backpack or in the players humanoid
		print("found tool in player")--prints just to be sure the tool was found
		
	end
	
end

end)

is this what do you mean??? or do you mean accesorry

I can’t use this in my script since it has nothing to do with .Touched, and its also not this specific since ill be picking multiple items rather than just 1 specific item from 1 specific place

do you mean you want to make it for a tool or for hat?

what I mean is that I can’t implement this into my script which i sent above

Use

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

This is the right and better way

2 Likes

It works for instances amd objects as well, normally it is used with dictionaries, also, the data you give to the loops in generated in dictionary form

2 Likes

this still doesn’t fix my issue which as I said its not related to pairs().

1 Like

You have to use ipairs for arrays.

2 Likes