Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I check if a player has something that is listed within a table?

So I'm currently making a Vest system for my game and the script that handles the system has a table that will list out multiple vests, and the script has to check if the player is currently wearing a vest that is listed within the table, but I have no idea how I would do that.

What I currently have

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		
		---PlayerHealth
		local MaxHealth = Humanoid.MaxHealth
		local Health = Humanoid.Health
		
		local Vests = {
			
			["TanVest"] = Character:GetChildren("TanVest"),
			["BlackVest"] = Character:GetChildren("BlackVest")
		}
		
		if Character:WaitForChild(Vests) then --I have gotten confused here on how I would check if a player has one of the 2 listed vests.
			local HealthAdd = Character:FindFirstChild(Vests).HealthAdd
			
			MaxHealth += HealthAdd.Value
			Health += HealthAdd.Value
		elseif not Character:WaitForChild(Vests) then --I have gotten confused here on how I would check if a player has one of the 2 listed vests.
			print("Player does not have a Vest.")
		end
	end)
end)

8 comments

0

Use FindFirstChild() instead of WaitForChild()

0

I tried this a bit ago even with the vest on it still returns "Player does not have a Vest.".

0

Why are you putting a string inside of the GetChildren function? GetChildren only returns a table of children. In your case Character:GetChilden() returns a table with all the children of the character model

0

findfirstchild(nameofyourvest not vests because vests is a table), getchildren() not Children("TanVest"),GetChildren("BlackVest")

0

MarkedTomato you wrong model:moveto() differ from humanoid:Moveto()

0

@lehoaiquoc248 What are you talking about. I'm saying that GetChildren has no parameters

0

@lehoaiquoc248"findfirstchild(nameofyourvest not vests because vests is a table), getchildren() not Children("TanVest"),GetChildren("BlackVest")" I don't understand what you mean. Explain please

0

@lehoaiquoc248" MarkedTomato you wrong model:moveto() differ from humanoid:Moveto()" This guy wants to check if a player has a vest. Why are you talking about this?

2 answers

✓ Accepted answer

This is a Script that is located in ServerScriptService

game.Players.PlayerAdded:Connect(function(client) -- When player is added to Players service
    client.CharacterAdded:Connect(function(char) -- when that player's character is added

        local Vests = -- Vest locations
{["TanVest"] = char:FindFirstChild("TanVest");
     ["BlackVest"] = char:FindFirstChild("BlackVest";
} -- Vest locations

        local charChildren = char:GetChildren() -- getting children of the character(This is a table by way it :GetChildren() returns a table specifically a dictionary)

        local blackVest = table.find(charChildren, Vests.BlackVest) -- looks through the character table which are objects for the black vest
        local tanVest = table.find(charChildren, Vests.TanVest) -- looks through the character table which are objects for the tan vest

        if blackVest then -- checks whether the player has a blackvest if true then it runs "Player has a black vest" You can put some code to increase the character's health
            print("Player has a BlackVest")
        elseif tanVest then -- if tan vest if found then
            print("Player has TanVest")
        else -- if no vests were found then
            print("Player has no vest")
        end
    end)
end)

Notes

GetChildren has no parameters. All it does is return a table of the Instance which is getting the Children Instance:GetChildren().

Example:

local Children = Instance:GetChildren()
print(Children) -- This returns a table in the output

5 comments

0

I tried it out I put a empty model named blackVest and it printed "Player has a BlackVest" So it should work.

0

What I'm looking for is a accessory not a model so it won't work(I did test it in studio)

0

It just randomly started working.

0

I found out why it wasn't working it was because of the local HealthAdd so when I fixed that your script worked

for index = #Vests, 0, -1 do
	if Character:FindFirstChild(Vests[index]) then
		--code
	end
end

or

for i,v in ipairs(Vests) do
	if Character:FindFirstChild(v) then
		--code
	end
end

does that work?

This discussion is preserved as it was. New answers, comments, and votes are closed.