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

How do I Check what is inside of the table?

Asked by
To0_ny 141
3 years ago

so I wanted to make an explosion, and whenever it hits someone, it gets their character name, and I run a check to see if their name was already in the table, if it already was, it will not damage them. However, it does not work, and it damages them multiple times. What do I do?

script:

local part = script.Parent
local partsHit = {"hi"}
local caster = script.Caster-- the person who casted the explosion

part.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    local charName = hit.Parent.Name
    if hum ~= nil and hum.Parent.Name ~= caster.Value then
        for i, thing in pairs(partsHit) do
            if thing ~= charName then
                hum:TakeDamage(10)
                table.insert(partsHit, #partsHit + 1, charName)
            end
        end
    end
end)

1 answer

Log in to vote
2
Answered by
TGazza 1336 Moderation Voter
3 years ago

try this:

local part = script.Parent
local partsHit = {}
local caster = script.Caster-- the person who casted the explosion

part.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    local charName = hit.Parent.Name
    if hum ~= nil and hum.Parent.Name ~= caster.Value then
        local isFound = table.find(partsHit,charName) ~= nil
        if isFound == false then
            print("Unknown human.... lets damage it... hue hue!!")
            hum:TakeDamage(10)
            table.insert(partsHit, #partsHit + 1, charName)
        else
            print("Found Human Friend, Time for some tea!")
        end
    end
end)

lua has an inbuilt table function named find or table.find(<Table>,<Search>,<IndexStart>) made for exactly this problem. You can read up on table functions here: https://developer.roblox.com/en-us/api-reference/lua-docs/table

Hope this helps! :~)

Ad

Answer this question