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

Why is my on touch script being fired when anything touches it?

Asked by 5 years ago

Here is the script:

script.Parent.Touched:connect(function(Part)
    if Part.Parent ~= "SmallShip" or "Cannon" or "Barrel" then
        if Part.Name == "CannonBall" or "Explosion" then
            script.Parent.Health.Value = script.Parent.Health.Value - 1
            print('Ship took damage!')
        end 
    end

    if script.Parent.Health <= 0 then
        script.Parent.Parent.Weld:Remove()
        print('Ship destroyed!')
    end
end)

For some reason it is printing the ship took damage when anything touches it, how do I fix this?

0
on line three, are you checking if it is not those parts or it is? seems like you want them to touch it. RetroGalacticGamer 331 — 5y
0
What he said. We cant troubleshoot an issue if we dont know what you want, especially if you dont even know what you want. DinozCreates 1070 — 5y

1 answer

Log in to vote
1
Answered by
Isaque232 171
5 years ago

Correct me if I'm wrong but I believe when you're using " or " at the if statement you should specify it as you did before, Example:

if Part.Parent ~= "SmallShip" or Part.Parent ~= "Cannon" or Part.Parent ~="Barrel" then

And It seems that you might want to use and instead of or since if your Part.Parent is "Cannon" and you don't want it to continue It'll still continue since It always checks if Part.Parent isn't "SmallShip" and the same thing to if your Part.Parent is "SmallShip" Since if Part.Parent is also not "Cannon" it'll continue. So basically in the end It'll always continue.

So you would just need to change the first and second loop, would be like this:

if Part.Parent ~= "SmallShip" and Part.Parent ~= "Cannon" and Part.Parent ~= "Barrel" then
if Part.Name == "CannonBall" or Part.Name == "Explosion" then

Hopefully that helps you with that issue. :)

Ad

Answer this question