Five Night's at Freddy's 2 Music Box System

Hello! I’ve recently been working on a Fully Functional FNAF 2 remake, and I’ve been extremely stumped on the Music Box system, where the music box winds down (As the real game does.) I’m only working with images for this project but I can’t seem to fathom a script that works or performs what I want It to do.

Can someone write me a script that can replicate this system, or maybe help me to write a script that can replicate this?

This Is my WindUp workspace. (Wind Is the white pie circle that allows the music box to wind down.)
image

Any assistance is greatly appreciated! :slight_smile:

–First, create the variables:
local BoxValue = 100
local DecreaseRate = 1
local IncreaseRate = 5
local IsPlayerClicking = false
local WindUp = (Wind up button path)

–Then, create a while loop
while BoxValue > 1 and not IsPlayerClicking do
wait(1)
BoxValue -= DecreaseRate
if BoxValue < 0 then
BoxValue = 0
end
end

–now let’s make the value rises when the player clicks the button>>
WindUp.OnMouseButton1Down:Connect(function()
IsPlayerClicking = true
if BoxValue < 100 then
BoxValue += IncreaseRate
end
if BoxValue > 100 then
BoxValue = 100
end
task.wait(1)
IsPlayerClicking = false
end)

Hope this helps!
This code isn’t perfect, bugs may be expected

1 Like

Thank you man!!! :slight_smile: would you want credit on the game for the help?

No need, its just some lines of code.

When you finish developing the game, if posible, send me the link, i would love test the game!
:smiley:

Sorry, but your code has VERY bad practices which will ruin any code:

while BoxValue > 1 and not IsPlayerClicking do
wait(1)
BoxValue -= DecreaseRate
if BoxValue < 0 then
BoxValue = 0
end
end

This code in the middle of script will COMPLETELY disallow any further script progression, so ANYTHING after that won’t work.

if BoxValue > 100 then
BoxValue = 100
end

Did you heard about this 3 functions: math.min(), math.max(), math.clamp()? They are cleaner way to limit numbers.

And, additionally, if your code passes WHILE loop, it will work only on clicks. As I know, music box in FNAF 2 should be held to wind up:

local RunService = game:GetService("RunService")
local WindButton = --your button here
local WindPower = 100 --time of box playing
local MaxWind = 100 -- max time
local Connections = {}

local Winding = false
Connections[1] = WindButton.MouseButton1Down:Connect(function()
    Winding = true
end)
Connections[2] = WindButton.MouseButton1Up:Connect(function()
    Winding = false
end)
Connections[3] = WindButton.MouseLeave:Connect(function()
    Winding = false
end)
Connections[4] = RunService.RenderStepped:Connect(function(delta)
    WindPower = math.clamp(WindPower + (Winding and 10 or -1) * delta, 0, MaxWind)
    --Replace 5 and -1 with desired values

    --Do your code to show winding, for example white pie which grows
    --and shrinks...
end)

Some possible questions:

  1. Connections = {} used to store RBXLScriptConnections, which SHOULD be disconnect as soon as you don’t need them in such way:
--Single connection:
Connection:Disconnect()
Connection = nil

--Table of connections
for i = 1, #Connections, 1 do
    Connections[i]:Disconnect()
end
Connections = {}
  1. Winding = true - variable used to determine if user is winding box rn
  2. There’s 2 functions on button which set Winding to false, because user may just slide off button to try bug game and make endless winding.
  3. Feel free to ask more questions if you don’t understand something.

And last: @ThatDudeExo you shouldn’t ask anyone to write you script. You can ask about how write it, help fix code, or just give hints. But not this:

1 Like

Hi thanks for the fix here! and yeah I was just struggling to come up with something to make this work, and realize that the whole image system is a pain. Thanks for the help! :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.