Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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

01game.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.
View all 24 lines...
0
Use FindFirstChild() instead of WaitForChild() lehoaiquoc248 23 — 3y
0
I tried this a bit ago even with the vest on it still returns "Player does not have a Vest.". vincentthecat1 199 — 3y
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 MarkedTomato 810 — 3y
0
findfirstchild(nameofyourvest not vests because vests is a table), getchildren() not Children("TanVest"),GetChildren("BlackVest") lehoaiquoc248 23 — 3y
View all comments (4 more)
0
MarkedTomato you wrong model:moveto() differ from humanoid:Moveto() lehoaiquoc248 23 — 3y
0
@lehoaiquoc248 What are you talking about. I'm saying that GetChildren has no parameters MarkedTomato 810 — 3y
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 MarkedTomato 810 — 3y
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? MarkedTomato 810 — 3y

2 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

This is a Script that is located in ServerScriptService

01game.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")
View all 22 lines...

Notes

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

Example:

1local Children = Instance:GetChildren()
2print(Children) -- This returns a table in the output
0
I tried it out I put a empty model named blackVest and it printed "Player has a BlackVest" So it should work. MarkedTomato 810 — 3y
0
What I'm looking for is a accessory not a model so it won't work(I did test it in studio) vincentthecat1 199 — 3y
0
It just randomly started working. vincentthecat1 199 — 3y
0
I found out why it wasn't working it was because of the local HealthAdd so when I fixed that your script worked vincentthecat1 199 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
1for index = #Vests, 0, -1 do
2    if Character:FindFirstChild(Vests[index]) then
3        --code
4    end
5end

or

1for i,v in ipairs(Vests) do
2    if Character:FindFirstChild(v) then
3        --code
4    end
5end

does that work?

Answer this question