What do you want to achieve?
A simple (very simple) alarm system.
What is the issue?
The script I wrote works…To some extent…
When I click the ‘activate’ button, it freezes my game for about a minute. Then it works.
When I click the ‘off’ button, nothing happens. (I also use boolValues)
What solutions have you tried so far?
Searched DevForum to no avail.
My ‘on’ code.
local alarm = script.Parent.Sound
local clickDetector = script.Parent.ClickDetector
local active = script.Parent.Parent.Active
function onClick()
active = true
while active == true do
alarm.Looped = true
alarm:Play()
end
end
clickDetector.MouseClick:Connect(onClick)
The ‘off’ code:
local clickDetector = script.Parent.ClickDetector
local active = script.Parent.Parent.Active
function onClick()
active = false
end
clickDetector.MouseClick:Connect(onClick)
You excessively play the “Alarm” sound, you need to wait until the sound is finished to play it again. Also, you don’t need the while loop as you set the Alarm sound to loop itself already. Secondly, you can’t use “script.Parent.Parent.Parent.Active” as a variable to try and change it as it will not actually change and is instead stored as a variable within the script.
local alarm = script.Parent.Sound
local clickDetector = script.Parent.ClickDetector
local active = script.Parent.Parent
function onClick()
active.Active = true
alarm.Looped = true
alarm:Play()
end
clickDetector.MouseClick:Connect(onClick)
local alarm = script.Parent.Sound
local clickDetector = script.Parent.ClickDetector
local active = script.Parent.Parent
function onClick()
active.Active = false
alarm:Stop()
end
clickDetector.MouseClick:Connect(onClick)
Your game freezes because you’ve entered an infinite loop without any tasking, so internally luau is dedicating everything it has to running that loop every tick. It will eventually exhaust itself and warn to console, which is when your game unfreezes.
A loop isn’t necessary for this application it looks like, this would be even simpler with a single script parented to Panicers with the following:
local alarm = script.Parent.Sound--move this audio instance to be a child of Panicers instead
script.Parent.On.ClickDetector.MouseClick:Connect(function()
alarm:Play()
end)
script.Parent.Off.ClickDetector.MouseClick:connect(function()
alarm:Stop()
end)