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

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

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

Answer this question