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

script ignoring if statement but doing function?

alright for this script right here the explosion does print the parts it hit that are not locked but it also runs the if statement even when the basePart.Parent.Name does not equal Blue or Red it also destroys anything that is within a model and give a parent is nil value error and workspace is locked error

script:

workspace.ChildAdded:Connect(function(obj)
	if obj:IsA("Explosion") then
		obj.Hit:Connect(function(basePart, distance)
			print(basePart.Name)
			if basePart.Parent.Name == "BlueBarrel" or "RedBarrel" then
				basePart.Parent:Destroy()
			end
		end)
	end
end)

1 answer

✓ Accepted answer

What you do in your code is a very common mistake new developers do when using "if" statements.

The thing with Lua (could apply to other languages as well) is that you need to provide what it should check.

Basically in your if statement here:

if basePart.Parent.Name == "BlueBarrel" or "RedBarrel" then

you check if the basePart's parent name is "BlueBarrel" or if "RedBarrel" exists. Since "RedBarrel" is not falsy (AKA. false, nil etc.), but exists, this will return true and the if statement will return true.

You probably thought that it would check if the basePart's parent name was "BlueBarrel" or "RedBarrel".

There is not much you have to change though:

if basePart.Parent.Name == "BlueBarrel" or basePart.Parent.Name == "RedBarrel" then
	-- Continue
end

3 comments

0

oh alright thank you so much man i would pay you if i could

0

Don't worry. No need to pay, I like helping people and seeing them getting happy when I help them is more than enough reward to me.

0

you can pay him in thankfulness lol

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