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
01 | game.Players.PlayerAdded:Connect( function (Player) |
02 | Player.CharacterAdded:Connect( function (Character) |
03 | local Humanoid = Character:WaitForChild( "Humanoid" ) |
04 |
05 | ---PlayerHealth |
06 | local MaxHealth = Humanoid.MaxHealth |
07 | local Health = Humanoid.Health |
08 |
09 | local Vests = { |
10 |
11 | [ "TanVest" ] = Character:GetChildren( "TanVest" ), |
12 | [ "BlackVest" ] = Character:GetChildren( "BlackVest" ) |
13 | } |
14 |
15 | 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. |
This is a Script that is located in ServerScriptService
01 | game.Players.PlayerAdded:Connect( function (client) -- When player is added to Players service |
02 | client.CharacterAdded:Connect( function (char) -- when that player's character is added |
03 |
04 | local Vests = -- Vest locations |
05 | { [ "TanVest" ] = char:FindFirstChild( "TanVest" ); |
06 | [ "BlackVest" ] = char:FindFirstChild( "BlackVest" ; |
07 | } -- Vest locations |
08 |
09 | local charChildren = char:GetChildren() -- getting children of the character(This is a table by way it :GetChildren() returns a table specifically a dictionary) |
10 |
11 | local blackVest = table.find(charChildren, Vests.BlackVest) -- looks through the character table which are objects for the black vest |
12 | local tanVest = table.find(charChildren, Vests.TanVest) -- looks through the character table which are objects for the tan vest |
13 |
14 | 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 |
15 | print ( "Player has a BlackVest" ) |
Notes
GetChildren has no parameters. All it does is return a table of the Instance which is getting the Children Instance:GetChildren()
.
Example:
1 | local Children = Instance:GetChildren() |
2 | print (Children) -- This returns a table in the output |
1 | for index = #Vests, 0 , - 1 do |
2 | if Character:FindFirstChild(Vests [ index ] ) then |
3 | --code |
4 | end |
5 | end |
or
1 | for i,v in ipairs (Vests) do |
2 | if Character:FindFirstChild(v) then |
3 | --code |
4 | end |
5 | end |
does that work?