Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i Tween a thirst GUI?

I am making a Survival game that includes hunger and thirst gui

here's the code for the hunger which works perfectly apart for the color change part

local HG = script.Parent
local num = HG:WaitForChild("Back"):WaitForChild("NumText")
local Text = HG:WaitForChild("Back"):WaitForChild("Text")
local Plr = game.Players.LocalPlayer


repeat wait() until Plr.Character

local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxHunger = 100
local DecreaseRate = 25 -- 8 seconds = -1 hunger
local HungerValue = HG.HValue

HungerValue.Changed:connect(function()
	Text:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0, 1,0), "Out", "Linear", .2, true)
end)

while wait(DecreaseRate) do
	if HungerValue.Value - 1 >= 0 then
		HungerValue.Value = HungerValue.Value - 1
	end
	
	HungerValue.Changed:connect(function()
	num.Text = HungerValue.Value
end)
	
	if HungerValue.Value == 0 then
		repeat wait(1)
			Hum.Health = Hum.Health - 2
		until HungerValue.Value > 0 or Hum.Health <= 0
	end
end

if HungerValue.Value < 41 then
Text.BackgroundColor = BrickColor.new("Daisy orange")
end

if HungerValue.Value < 21 then
Text.BackgroundColor = BrickColor.new("Persimmon")
end

if HungerValue.Value < 11 then
Text.BackgroundColor = BrickColor.new("Really red")
end


and here is the script of the thirst which is somehow defective



--First the variables
local Thirst = script.Parent
local Thirstval = Thirst.Value
local Back = Thirst:WaitForChild("Background")
local Tbar = Back:WaitForChild("Bar")
local Plr = game.Players.LocalPlayer
local hum = Plr.Character.Humanoid
local MaxThirst = 100
decreaserate = 30
local num = Thirst.Label
--wait till the character loads
repeat wait() until Plr.Character

while wait(decreaserate) do
	if Thirstval.Value - 1 >= 0 then
		Thirstval.Value = Thirstval.Value - 1    --reduce if it isnt 0 or below
	end 
	Thirstval.Changed:connect(function()
		num.Text = Thirstval.Value     --update the text indicator
	end)
	if Thirstval.Value == 0 then
		repeat wait(2.5)
			hum.Health = hum.Health - 2.5   --reduce the health if its 0
		until Thirstval.Value > 0 or hum.Health <= 0
		end
end

Thirstval.Changed:connect(function()
		Tbar:TweenSize(UDim2.new(Thirstval.Value/MaxThirst,0, 1,0),"Out","Linear", .2, true) --tween the bar indicators
	end)




-- this is frustrating

I don't get why it isn't working, can someone pls tell me what I'm doing wrong?

3 comments

0

Please don't post in all 79 lines of code. Give us what you think are the problem parts of the code. The entire hunger code is irrelevant if we're focusing on your thirst code. You also need to tell us how the code is failing. Is it working up until the tweening? Is it just not working at all? More information like this would be helpful.

0

Ok thnx Im new to this website

0

and in line 11 i meant 15 secs

2 answers

✓ Accepted answer

My method for tweening a thirst GUI would be to make a frame, scale it to the size of a bar, call it BG, then create another one and put it inside 'BG', then you can change the size of it using a script, something along the lines of

(let's say your max thirst is 100 and the bar we want to tween is called 'Scale')

scale:TweenSize(UDim2.new(thirst/100,0,1,0))

So basically thirst/100 is going to give you a number between 0 - 1, 1 being a full thirst bar, 0 being an empty thirst bar

so if a player has a thirst of 57, 57/100 is 0.57, so it'll be just over half filled

There are few problems with your code. Consider this expression: wait(decreaserate). If you read about wait(), you will find out that it returns actual time elapsed before the task scheduler resumes your code. So its never false nor nil, therefore you have the infinite loop there. This is causing two serious issues.

First issue - following code will never be executed:

Thirstval.Changed:connect(function()
        Tbar:TweenSize(UDim2.new(Thirstval.Value/MaxThirst,0, 1,0),"Out","Linear", .2, true) --tween the bar indicators
    end)

Connection is never made, because the interpreter is stuck in this infinite loop.

Second issue - you are making new connections for the same function everytime the loop is repeated:

    Thirstval.Changed:connect(function()
        num.Text = Thirstval.Value     --update the text indicator
    end)

This small function connected multiple times will probably not cause significant lag, but after a while you may start to notice flickering. Remember, you only make connections ONCE! Lua will not throw an error for repeating connections, because more than one connection is allowed.

2 comments

0

pls explain more, tell me why the first one wont work im not excactly a pro at scripting i dont even understand for loops :3

0

and that decrease rate stuff worked for the first one

This discussion is preserved as it was. New answers, comments, and votes are closed.