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

Why is this :GetTouchingParts - script printing the wrong thing?

Asked by
nanaluk01 247 Moderation Voter
7 years ago
Edited 7 years ago

Why is thiis :GetTouchingParts - script printing the wrong thing?

What I want it to do is not print the name of what touched the part(s), if the part(s) that touched it has a certain name(see script)

Why isn't this working?

Any help is greatly appreciated!

for i,v in pairs(cloneModel:GetChildren()) do
    if v.ClassName == "Union" or v.ClassName == "Part" then
        local parts = v
        v.Touched:Connect(function()
            local touching = parts:GetTouchingParts()
            for _,p in next, touching do
                if p.Name ~= "Left Leg" or p.Name ~= "Right Leg" or p.Name ~= "Left Arm" or p.Name ~= "Right Leg" or p.Name ~= "Head" then
                    print(p.Name..' is touching '..parts.Name..'!')
                end
            end
            print()
        end)
    end
end

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

This is happening because you're using or instead of and in your if statement; you only want it to only run if all of those conditions are true, not just (at least) one. This can easily be remedied:

for i,v in pairs(cloneModel:GetChildren()) do
    if v.ClassName == "Union" or v.ClassName == "Part" then
        local parts = v
        v.Touched:Connect(function()
            local touching = parts:GetTouchingParts()
            for _,p in next, touching do
                if p.Name ~= "Left Leg" and p.Name ~= "Right Leg" and p.Name ~= "Left Arm" and p.Name ~= "Right Leg" and p.Name ~= "Head" then
                    print(p.Name..' is touching '..parts.Name..'!')
                end
            end
            print()
        end)
    end
end

However, there are other ways to go about doing this, such as using a table, which allows it to be a bit more organised:

local ignoreList = {
    ['Left Leg'] = true,
    ['Right Leg'] = true,
    ['Left Arm'] = true,
    ['Right Arm'] = true,
    ['Head'] = true,
};
for i,v in pairs(cloneModel:GetChildren()) do
    if v.ClassName == "Union" or v.ClassName == "Part" then
        local parts = v
        v.Touched:Connect(function()
            local touching = parts:GetTouchingParts()
            for _,p in next, touching do
                if not ignoreList[p.Name] then
                    print(p.Name..' is touching '..parts.Name..'!')
                end
            end
            print()
        end)
    end
end

Hope this helped.

Ad

Answer this question