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)
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
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?