What am I doing wrong in this script

local parent = script.Parent
local pipe = script.Parent["DISTORTION'S STAFF"]
local colorRNG = math.random(1,5)
local explosion = script.Parent.Explosion
--Sounds
local sound1 = explosion["Scary Sound"]
local tableofNames = {["Explosion"] = true,
	["shockwave"] = true,
	["invispart"] = true,
	["INVISIBLEBOTTOM"] = true,
	["PipeSmash"] = true,
	["DISTORTION'S STAFF"] = true
}


while true do
	for i = 1,25 do
		pipe.Transparency = pipe.Transparency - 0.04
		task.wait(0.01)
	end
	--Creating Beam
	task.wait(0.25)
	pipe.Transparency = 0
	local debounce = false
	local attachment = Instance.new("Attachment", pipe)
	local vectorForce = Instance.new("VectorForce", pipe)
	vectorForce.Parent = parent
	vectorForce.ApplyAtCenterOfMass = true
	vectorForce.Enabled = false
	vectorForce.Attachment0 = attachment
	local allignPosition = Instance.new("AlignOrientation", pipe)
	allignPosition = attachment
	local lookVector = pipe.CFrame.LookVector
	local debounce = false
	for i = 1,500 do
		pipe.Anchored = false
		parent.PrimaryPart.Anchored = false
		vectorForce.Enabled = true
		pipe.Velocity = lookVector * 185
		pipe.Touched:Connect(function(Part)
			if debounce == false then
				print(Part.Name)
				debounce = true
				if Part.Name == tableofNames[(tableofNames)] then	
				elseif Part.Name and not tableofNames[(tableofNames)] then
					local pipeexplosion = explosion:Clone()
					pipeexplosion.Parent = workspace
					pipeexplosion.Position = pipe.Position
					pipeexplosion.Script.Enabled = true
					pipeexplosion.ColorScript.Enabled = true
					sound1:Play()
					pipe:Destroy()
				end
			end
		end)
		task.wait(0.01)
	end
	parent:Destroy()
        task.wait(1234567898765434567654345676543)
end

For context, I’m trying to make a part that collides with another part and explodes. That part works, however I want it so that the part (pipe) cannot trigger the function from certain part names.

In other words, it’s this line of code that I’m having trouble with.

pipe.Touched:Connect(function(Part)
			if debounce == false then
				print(Part.Name)
				debounce = true
				if Part.Name == tableofNames[(tableofNames)] then	
				elseif Part.Name and not tableofNames[(tableofNames)] then
					local pipeexplosion = explosion:Clone()
					pipeexplosion.Parent = workspace
					pipeexplosion.Position = pipe.Position
					pipeexplosion.Script.Enabled = true
					pipeexplosion.ColorScript.Enabled = true
					sound1:Play()
					pipe:Destroy()
				end
			end
		end)

Here’s what’s wrong, it’s not printing any errors in the code but when I load the game, the part explodes on the parts I specifically blacklisted from their names from tableofNames, which is not what I want. I do not know what I’m doing wrong and I’ve also tried multiple attempts to solve the problem, but they have failed so now I’m asking for help because of that part. If anyone would like to help, please do! I’d greatly appreciate it!

You need to run a for loop when your checking if the parts name is one of the tableofNames. It won’t check the table otherwise.

local value = false
for i, part in pairs(tableofNames) do 
if Part.Name = part then 
value = true 
end
task.wait() 
if not value then 
-- your code here

Probably has an error I haven’t coded in over 6 months but that is roughly what you need to do.

What is the local value = false supposed to do? I’m not sure why I should use it.

I’ve tried the code. It didn’t work, the part exploded in the specifically blacklisted parts.

tableofNames[(tableofNames)]

You didn’t properly index it it should be :

tableofNames[Part.Name]

I’ve also done that, but that didn’t work. That was part of the many solutions I did that didn’t work.

Edit: Thought too fast, it worked. Although placing the same model together bugged the part where the script would clone the part when it passed the condition that the part’s name is not the blacklisted name.

Update: So now that leads me to my 2nd issue.

pipe.Touched:Connect(function(Part)
			if debounce == false then
				print(Part.Name)
				debounce = true
				if Part.Name == tableofNames[Part.Name] then	
				elseif Part.Name and not tableofNames[Part.Name] then
					local pipeexplosion = explosion:Clone()
					pipeexplosion.Parent = workspace
					pipeexplosion.Position = pipe.Position
					pipeexplosion.Script.Enabled = true
					pipeexplosion.ColorScript.Enabled = true
					sound1:Play()
					pipe:Destroy()
				end
			end
		end)

Unforunately sorry if I’m dragging on to this too much but this is another side effect. After the part touches the blacklisted name of the part, the part after touching it stops working and even when it touches a non-blacklisted part from tableofNames the part where it clones explosion stops working. How do I fix this?

1 Like

You never set ‘debounce’ back to false

Edit : typo

Tried it again, really sorry for the quick assumption. It worked, it’s just that I thought that the amount of clones being produced meant that the code didn’t work, although that can easily be fixed by setting value to true in the if statement of if not value then. I apologize to others that I dragged on, since the original fix by you did work, it’s just that I quickly assumed.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.