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

Making a Sprinkler System?

Ive based my script on ROBLOX's extinguishing fire extinguisher gear which worked, but now that I have heavily modified the model I have forgotten which is which. Does anyone know what is wrong with it? Here is my code:

local Tool = script.Parent
local spraying = false
local offset = 5
local debris = game:GetService("Debris")

local bubble = Instance.new("Part")
bubble.Shape = 0
bubble.formFactor = 0
bubble.Size = Vector3.new(1,1,1)
bubble.Transparency = 0.2
bubble.BottomSurface = "Smooth"
bubble.TopSurface = "Smooth"
bubble.CanCollide = true

SP = script.Parent
FP = game.Workspace.FireAlarmPanel

FP.Status.Changed:connect(function()
	if FP.Controller.FiredoorDiscon.Value == true then return end
	if FP.Status.Value == 1 or 2 then
		sprayBubbles()
	if FP.Status.Value == 0 then
		print "Sprinkler Armed"
	end
end

function sprayBubbles()
		
		local sprayDir = script.Parent.w.CFrame.p.unit
		local bubbleClone = bubble:clone()
		local torsoNormal = script.Parent.w.CFrame.p
		local denom = math.abs(torsoNormal.x) + math.abs(torsoNormal.z)
		local posX = 5 * (torsoNormal.x/denom)
		local posZ = 5 * (torsoNormal.z/denom)
		bubbleClone.Position = Vector3.new(Tool.Handle.Position.x + posX,Tool.Handle.Position.y,Tool.Handle.Position.z + posZ)
		bubbleClone.Velocity = Vector3.new(sprayDir.x,sprayDir.y + 0.5, sprayDir.z) * 50
		bubbleClone.Size = Vector3.new(math.random(1,2),math.random(1,2),math.random(1,2))
		bubbleClone.Parent = game.Workspace

		local script = Tool.ExtinguishScript:clone()
		script.Parent = bubbleClone
		script.Disabled = false

		debris:AddItem(bubbleClone, 2)
		
		wait(0.05)
	end

end

Sorry if I am a noob to scripting, I'm not very good at it. Thanks!

~Stewie140

1 answer

On line 21, you called a function before it was defined. In Lua, you can't do that. Move the sprayBubbles() function to be before your connection function.

Also, you have problems with end's. An end is used to tell the script that the Code Block is over.

For example:

if (true) then
	--This in here is the Code Block that happens if the conditional in between "if" and "then" is true.

	-- In this case, the conditional is true, so this code runs!

end -- This tells the code the Code Block is over.

print("Hi") -- This is outside the Code Block. 

-- This will execute whether the if statement executes or not, because it is not a part of that Code Block

function sprayBubbles() --Create a function that runs when the script calls for "sprayBubbles()"

	-- code

	--code

end -- This is the end of the code that runs when you call spray bubbles

print("Hello World") -- This will not run whenever sprayBubbles is called, because it is not a part of that Code Block

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