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

Can I Add to UDim2?

So I Edited part of a script from food, to make the hunger bar go up when you eat it.

It works, but I only found a way to make it so that it fills the hunger bar all the way.

So is there any way to add a certain amount to the UDim2 property of the Gui, and make it so it doesn't go above what it is normally?

This is the chunk of code where it adds full thirst

local character = Tool.Parent
    local player = game.Players:GetPlayerFromCharacter( character )

	local thirst = player.PlayerGui.Thirst
	
	if (thirst ~= nil) then
		thirst.BG.Bar.Size = UDim2.new(1, 0, 1, 0)

1 answer

✓ Accepted answer

Well if you want to make the bar go lower, you would do something like this

local maxthirst = 100 --create a max thirst value
local currentthirst = 100 --create a currentthirst value

local currentthirstpercent = currentthirst/maxthirst --divide currentthirst by maxthirst to create a value between 0 and 1 to represent the percentage of thirst left.

function updatebar() --a function that will update the thirst bar. run this whenever you change the thirst
	local currentthirstpercent = currentthirst/maxthirst --re-divides the thirst values
	thirst.BG.Bar.Size = UDim2.new(currentthirstpercent,0,1,0) --sets the size in the x of the bar to the percentage of thirst remaining
end

function drink(amount) --example function for increasing thirst
	currentthirst = currentthirst + amount --add thirst together
	if currentthirst > maxthirst then --checks to see you aren't over the max
		currentthirst = maxthirst --if you're over the max it sets you to the max
	end
	function updatebar() --updates the bar because you changed the value of thirst
end

This is all example and you'd need to code things like decreasing thirst values and such as well, but this is what you're asking for I think.

2 comments

0

I like the script, but It's hard to insert it into the other script, I usually have trouble where to put 'ends' and stuff

0

Just use the general idea to write your own. That's usually the best way to figure out how the code works is to rewrite it. Also you should have an end for every if, for, while, or function. Put ends at the end of the statement, kind of like a period.

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