How would I share values between scripts?

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?

5 Likes

Are they the same type of script (Script/Script or LocalScript/LocalScript) or are they different (LocalScript/Script)?

1 Like

The light management script (Where I get the blackout value from) is a script, the other 2 are localscripts.

1 Like

These are supposed to be variables from another script?

Yes, those are meant to be the sanity values from the first script.

Well you can’t just referece variables like that. Use NumberValues or IntValues.

1 Like

How would I use those in this context? By the way, is there also one of those for booleans as I need to get the value of the blackoutActive boolean.

Is your sanity manager in Player.PlayerScripts?

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.

  1. Create 2 NumberValues parented to the sanity manager script - Sanity and MaxSanity

  2. 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)

5 Likes

Will this cause the values of the Sanity and max Sanity to still be client sided?

if u create the number values on client yes it will be client sided
if u are creating the values on server its server sided

What do you mean by creating them on server/client?

Like if u create any instance in a LocalScript its gonna be client sided
if u create it in a regular script its gonna be on the server

1 Like

There is no need to create the values in the code. Just create them in the Explorer.

Also, the values will be server sided. Even though client can change them, the effect will only be visual.

1 Like

Would it be possible to parent the sanity and max sanity values to the player and have them save?

1 Like

ye ur able to do that
aaaaaaaaaaaaaaaaaaa

1 Like

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