Attempting to make a pet "happiness bar"

Hello, I’m creating a game and I’d like some support on making a happiness bar for your pet.
Basically it would decrease by 1 every few seconds until 0.
I am new to scripting so i’d like some help please.

simple happiness script

local Happiness = 100
local ReductionRate = 1 -- the seconds waiting before reducting

while Happiness > 0 do
	Happiness -= 1
	task.wait(ReductionRate)
end
print("not happy")
local Happiness = 100
local ReductionRate = 0.5 -- the seconds waiting before reducting

local function Die()
	print("oh no i died")
end

local function DoSomething()
	-- yay so happy right now guys
	Happiness = math.clamp(Happiness + 50, 50, 100)
end

while Happiness > 0 do
	Happiness -= 1
	task.wait(ReductionRate)
end
Die()

How would I incorporate this into a frame/gui so it would be like a health gui that decreases?

Is this a LocalScript or ServerScript?

Frame.Size = Udim2.new(Happiness / 100, 0, Frame.Size.Y.Scale, Frame.Size.Y.Offset)

I’ve tried adding this

local ReductionRate = 1 -- the seconds waiting before reducting
local Frame = script.Parent.Frame

while Happiness > 0 do
	Happiness -= 1
	task.wait(ReductionRate)
	Frame.Size = UDim2.new(Happiness / 100, 0, Frame.Size.Y.Scale, Frame.Size.Y.Offset)
end
print("not happy")

But it doesn’t do anything, did I do something incorrectly? I put it into a localscript inside a ScreenGUI.

you forgot to add the Happiness variable probably

local Happiness = 100
local ReductionRate = 1 -- the seconds waiting before reducting
local Frame = script.Parent.Frame

while Happiness > 0 do
	Happiness -= 1
	task.wait(ReductionRate)
	Frame.Size = UDim2.new(Happiness / 100, 0, Frame.Size.Y.Scale, Frame.Size.Y.Offset)
end
print("not happy")

Sorry it still doesn’t work.
This is how my screengui looks like,
image

You forgot to rename “frame” as “happinessbar”, Also add a “print(Happiness)” to see if it changes in

local Happiness = 100
local ReductionRate = 1 -- the seconds waiting before reducting
local Frame = script.Parent.HappinessBar

while Happiness > 0 do
	Happiness -= 1
        print(Happiness)
	task.wait(ReductionRate)
	Frame.Size = UDim2.new(Happiness / 100, 0, Frame.Size.Y.Scale, Frame.Size.Y.Offset)
end
print("not happy")

Your frame seem to not have a frame within it, add a frame with in it and put the frame at, (1,0) x and (1,0) y in terms of Size

I have already renamed frame as happinessbar.
But I do not know what you mean to add a frame with in a frame…

It doesn’t matter, I read the script wrong.

Hi,
Do not directly use scripts you don’t know how works. Here I would like to classify, that you don’t have to know every bit of a script, but it’s good to at least know the few core bits.

What is your issue so far, with the script you’ve been presented?

A frame within a frame is exactly that. You want a ParentFrame that determines the length & height of the “happiness” frame is in full form. Then you insert a child-frame into ParentFrame. It’s the child-frame that you resize, with the following line that you’ve been presented;

ChildFrame.Size = UDim2.new(CurrentHappiness/MaxHappiness, 0, 1, 0)

Edit: Actually edited the line above. Since you have a ParentFrame, you can do it as simple as that.

Hello, I dont think i fully understand you, but I made the parent frame “HappinessBar” red and added a childframe into it that is green. Ive tried adding your script but still, no outcome.
image
image

Hi, I did not quite deliver you a completed script.

You gotta show us what you have within your localscript, since a image (or a copy/paste) of the current code tells us more than a thousand words.

local ReductionRate = 1 -- the seconds waiting before reducting
local Frame = script.Parent.HappinessBar
local ChildFrame = Frame.ChildFrame

while Happiness > 0 do
	Happiness -= 1
	task.wait(ReductionRate)
	ChildFrame.Size = UDim2.new(Happiness / 100, 0, 1, 0)
	print(Happiness)
end
print("not happy")```

Your setup in your explorer looks alright as of now.

Can you show me the properties of ChildFrame & HappinessBar?



Is this what you mean?

There is a better way and also make the happiness go up is,

local Happiness = 100
local ReductionRate = 1 -- the seconds waiting before reducting
local Frame = script.Parent.HappinessBar
local ChildFrame = Frame.ChildFrame
game:GetService("RunService").RenderStepped:Connect(function()
        if something that is not making the pet happy and not something making your pet happy and 
        Happiness > 0 then
	    Happiness -= 1
        print(Happiness)
	    task.wait(ReductionRate)
	    Frame.Size = UDim2.new(Happiness / 100, 0, Frame.Size.Y.Scale, Frame.Size.Y.Offset)
        else if something that makes the pet happy and not something making your pet sad and Happiness <= 100 then
        happiness += 1
        print(Happiness)
        task.wait(ReductionRate)
        ChildFrame.Size = UDim2.new(CurrentHappiness/MaxHappiness, 0, 1, 0)
        end
        if not Happiness > 0 then
         print("not happy")
         task.wait(ReductionRate)
        end
end)

I believe you should learn more about scripting for now, check Roblox Documents and then learn something from Youtube or the documents. Use this Script if you understand it.

Set Size for ChildFrame to: 1,0,1,0.
After that, try this script:

local MaxHappiness = 100 -- Defines MaxHappiness
local Happiness = MaxHappiness -- Initializes default happiness
local ReductionRate = 1 -- Defines how many seconds to wait before looping again
local HappinessBar = script.Parent:WaitForChild("HappinessBar")
local ChildFrame = HappinessBar.ChildFrame

while Happiness > 0 do
	Happiness -= 1
	ChildFrame.Size = UDim2.new(Happiness/MaxHappiness, 0, 1, 0)
	task.wait(ReductionRate)
end

When all this is said, I would not recommend this type of script in the long run, since this can be very ineffective. But since you’re a beginner, there is no need to make things more complicated for you.

This script still didnt work. No errors.