So with this script i attempted to make a dam functional by
- Moving bricks up or down to simulate a grate/water gate being activated
- Changing the colour of the button to neon when grates are retracted.
- 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