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

why am I not able to Connect the functions here?

local sirenOn = game.Workspace.SirenOn
local sirenOff = game.Workspace.SirenOff
local lockDown = game.Workspace.LockDown
local alarm = game.Workspace.Alarm

while wait(0.2) do
	if lockDown.Status.Value == true then
		print("yeet")
		
		sirenOff.ClickDetector.MouseClick:Connect(StopSiren)
		print("function called")
			
		sirenOn.ClickDetector.MouseClick:Connect(PlaySiren)
	end
end
	

function PlaySiren()
	alarm.Sound:Play()
end

function StopSiren()
	alarm.Sound:Stop()	
end

Error: 21:12:26.685 - Attempt to connect failed: Passed value is not a function

1 comment

0

Try putting the functions before you call them, like physically place them above the while loop.

2 answers

Because the functions you made to play or stop the Siren are placed after the loop. You need to place both of them before so that your loop can recognize them.

Like This,

local sirenOn = game.Workspace.SirenOn
local sirenOff = game.Workspace.SirenOff
local lockDown = game.Workspace.LockDown
local alarm = game.Workspace.Alarm

function PlaySiren()
    alarm.Sound:Play()
end

function StopSiren()
    alarm.Sound:Stop()  
end

while wait(0.2) do
    if lockDown.Status.Value == true then
        print("yeet")

        sirenOff.ClickDetector.MouseClick:Connect(StopSiren)
        print("function called")

        sirenOn.ClickDetector.MouseClick:Connect(PlaySiren)
    end
end

Hope this helped! -Swifty

1 comment

0

There's no point in having a loop.

-- You can shorten game.Workspace to workspace
local sirenOn = workspace.SirenOn
local sirenOff = workspace.SirenOff
local lockDown = workspace.LockDown
local alarm = workspace.Alarm

-- Anonymous functions, you don't need a loop. 
sirenOn.ClickDetector.MouseClick:Connect(function()
	if not lockDown.Status.Value then 
		alarm.Sound:Play()
	end
end)

sirenOff.ClickDetector.MouseClick:Connect(function()
	if lockDown.Status.Value then 
		alarm.Sound:Stop()
	end
end)

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