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

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

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?

2 comments

0

on line three, are you checking if it is not those parts or it is? seems like you want them to touch it.

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.

1 answer

✓ Accepted answer

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

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