I’m trying to make a panic meter but I’m having a hard time getting it to work correctly.
For some context I want it to be so that when the lights go out, the meter will fill up and vice versa.
It is a local script inside of StarterCharacterScripts.
I’m having a few different issues. One of them is where the meter will work correctly until the lights go out a second time or it won’t go down when the lights turn on. Another issue I’m having is where the bar fills up instantly and will not go back down.
This is what the part of the script that doesn’t work looks like now
What am I doing wrong?
local RS = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local playerGui = player.PlayerGui
local panic = 0
local maxPanic = 1000
local drainRate = 1
local refreshRate = 1
local panicRefresh = 0
local detect = game.Workspace:WaitForChild("detect")
local lightsOff = rStorage:WaitForChild("lightsOff")
local lightsOn = rStorage:WaitForChild("lightsOn")
local function updateUI()
playerGui.Sanity.SanityFrame.SanityBar.Size = UDim2.new(math.clamp(0 / maxPanic, 0, 1), 0, 1, 0)
end
lightsOff.OnClientEvent:Connect(function(player)
RS.Heartbeat:Connect(function(deltaTime)
panic = math.max(maxPanic, panic + drainRate * deltaTime)
updateUI()
if panic == 1000 then
hum.Health = 0
end
end)
end)
lightsOn.OnClientEvent:Connect(function(player)
RS.Heartbeat:Connect(function(deltaTime)
panic = math.min(0, panic - refreshRate * deltaTime)
if panic <= 1000 then
updateUI()
end
end)
end)