Help fixing a dam system script

So with this script i attempted to make a dam functional by

  1. Moving bricks up or down to simulate a grate/water gate being activated
  2. Changing the colour of the button to neon when grates are retracted.
  3. Enabling a water particle emitter when the grates are retracted.

It worked until i added the two for loops for the and local variables for the particle emitter part of the script. Now when i click the button the function gets as far as the elseif statement, so i am unable to put the grates back down and the particle emitter remains constant.

local _items = {} 


local door = 1 
--				-1 = Closed
--				0 = Active
--				1 = Open

function getItems() 
	for _,v in pairs(script.Parent:GetChildren()) do 
		if ((v:IsA("BasePart")) and (v.Name == "Grate")) then 
			table.insert(_items,v) 
		end 
	end 
end 

function main() 
	local waterName = "Emitter"									--name of the brick holding the particle emitter
	local emitterName = "Water"									--name of the particle emitter
	local model = script.Parent
	local d = door 
	door = 0 
	script.Parent.Button.Material = Enum.Material.Neon    		--lights up button
	for i = 1,40 do 											-- moves grate upwards
		for _,v in pairs(_items) do 
			v.CFrame = v.CFrame * CFrame.new(0,(0.20*(-d)),0) 
		end 
		wait() 
	end 
	if (d == (-1)) then 
		for i, water in pairs(model:GetChildren())do			--for loop doesn't seem to work
			if water.Name == waterName then
			water[emitterName].Emission.Enabled = true			--meant to disable the particle emitter named water
			end
		end
		script.Parent.Button.Material = Enum.Material.Neon
	elseif (d == 1) then 											-- elseif doesn't work with the for loop included, breaks at this point
		script.Parent.Button.Material = Enum.Material.SmoothPlastic
		for i, water in pairs(model:GetChildren())do
			if water.Name == waterName then
			water[emitterName].Emission.Enabled = true					
			end
		end
	end 
	door = d 
end 

script.Parent.Button.Click.MouseClick:connect(function() 
	if (door == 0) then return end 
	door = (door*(-1)) 
	main() 
end) 

Any ideas on how to fix this are appreciated, thanks :slightly_smiling_face:

Can you put a print in the main for door.
try changing the if (d == (-1)) then to if (d == -1) then. might just be me but it looks wrong.

Looks to me like you need to flip your d variable. It never changes from 1 to -1.

EDIT - I was mistaken here, sorry about that. One issue that I see is that in both of your loops you’re setting the Emission’s Enabled property to true so that would explain why your particle emitters never stop.

I think we do need a bit more info as to what is happening. In the comments in your code you mentioned that it’s breaking at the elseif, but what exactly do you mean there? Is it throwing an error or is it just not doing something?

1 Like

From what I see here, you changed your door variable to have value 0 when you click the button the first time. So when you click the button again, it will never become 1 or -1, since 0*-1 or 0*1 will equal 0.

Hi there! Does the output show any error?
I’m asking because if the ParticleEmitter is named “Water” (emitterName string), then “Emission” is not a valid member of water. Try water[emitterName].Enabled instead of water[emitterName].Emission.Enabled.

1 Like

The door variable is set to 0 in main and then set to d so it won’t be 0 when the MouseClick event fires.

1 Like

Yeah that fixed it, thanks. I was unsure at first what Enabled should be called as there are two in a particle emitter under Behaviour and Emissions properties sections.

1 Like