Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Touch script not allowing more than 1 part?

Hello I have this script that I made in my submarine game.

function onTouched(hit)
    if hit.Name == "rocket" then
        local tere=script.parent.pohi.customPhysicalProperties
script.Parent.pohi.CustomPhysicalProperties = PhysicalProperties.new(tere.Density + 0.1, 0, 0)
    end
end

script.Parent.Touched:connect(onTouched)

I have 2 problems with it. First right now it works if a rocket touches it but I also need it to work if a torpedo touches it like this: if hit.Name == "rocket" and "torpedo" then. The problem is that when I insert the or torpedo text then it will react to everything for example terrain water which is bad for me.

THe second problem is that I want this script to apply to every part in a model and I tried to make it so but I failed. So if anyone could help me with this I would be grateful. Thanks for your time!

1 answer

✓ Accepted answer

#Your first issue

The and and or don't work like that.

if hit.Name == "rocket" or "torpedo" then

It doesn't work like you think it would, you need to do this

if hit.Name == "rocket" or hit.Name =  "torpedo" then

Yeah I hope you got the picture.

And if you're wondering why it was working with any part its because of that or "torpedo" since if statments will pass if the condition is true or if there is any sort of value that isn't nil.

if true then
print("hi") --prints
end

if 5 then
print("hi") --prints hi because as I said any value put in like 5 for examples will make the if statment work
end

if "cooool" then
print("hi") --it even prints here, because we have a string value in place of the condition
end

if nil then
print("hi") --doesn't print here
end

if false then
print("hi")    --doesn't print here
end

And since you said if hit.Name == "rocket" or "torpedo" then which means just one of those 2 conditions has to be true or any other value besides nil or false, and we had "torpedo" just a lonely string sitting there, this if statment will pass no matter what

#Second Issue Well for the second issue, instead of checking for each parts of the model if a torpedo or rocket touched, what you can do instead is, have a huge invisible part, that will cover the submarine and you can say that this part is the "hitbox" of that submarine, so just check if this part was touched by a torpedo or a rocket

This discussion is preserved as it was. New answers, comments, and votes are closed.