I’m currently trying to code a Sanity system and a Sanity bar for my Backrooms game and have these scripts. This script manages the Sanity value:
local Sanity = 100
local maxSanity = 100
blackout = game.ServerScriptService.LightManager.blackoutActive
if Sanity > maxSanity then
Sanity = maxSanity
end
while true do
if Sanity > 0 then
print(Sanity)
task.wait(5)
Sanity -= 1
end
end
I have the blackout variable because I was trying to detect when there’s a blackout active to increase sanity drain. Here’s the script for updating the Sanity bar:
local TW = game:GetService("TweenService")--Get Tween Service
local Player = game:GetService("Players").LocalPlayer --Get The Player
local Character = Player.Character or Player.CharacterAdded:Wait() --Wait For The Player Humanoid
local Humanoid = Character:WaitForChild("Humanoid") --Get The Player Humanoid
local sanity = Player.PlayerScripts.Sanity.Sanity
local maxSanity = Player.PlayerScripts.Sanity.maxSanity
local sanityBar = script.Parent
local function updateSanityBar() --Health Bar Size Change Function
local sanityPercent = math.clamp(sanity / maxSanity, 0, 1) --Maths
local info = TweenInfo.new(sanity / maxSanity,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0) --Tween Info
TW:Create(script.Parent,info,{Size = UDim2.fromScale(sanityPercent, 1)}):Play() -- Create The Tween Then Play It
sanityBar.TextLabel.Text = "Sanity: ".. sanity.. "/".. maxSanity
end
while true do
updateSanityBar()
end
When this script runs it returns the error “Sanity is not a valid member of (playername).PlayerScripts.Sanity” How would I go about allowing the Sanity bar UI script to detect the player’s current Sanity and max Sanity and letting the Sanity script detect blackouts?
Yes, that’s exactly where it is. The Lighting script where the blackoutActive boolean is is in ServerScriptService and the Sanity bar script is located in the Sanity bar UI.
Create 2 NumberValues parented to the sanity manager script - Sanity and MaxSanity
Reference them in scripts (and set their initial values). Also notice that every time I call for their value I add .Value at the end of the name.
local Sanity = script:WaitForChild("Sanity")
local MaxSanity = script:WaitForChild("MaxSanity")
blackout = game.ServerScriptService.LightManager.blackoutActive
if Sanity.Value > MaxSanity.Value then
Sanity.Value = MaxSanity.Value
end
while true do
if Sanity.Value > 0 then
print(Sanity.Value)
task.wait(5)
Sanity.Value -= 1
end
end
Same goes for the local script (you can use Ctrl+F in order to find and replace all sanity and maxSanity with sanity.Value and maxSanity.Value)