Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can I Add to UDim2? [closed]

Asked by 10 years ago

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

1local character = Tool.Parent
2    local player = game.Players:GetPlayerFromCharacter( character )
3 
4    local thirst = player.PlayerGui.Thirst
5 
6    if (thirst ~= nil) then
7        thirst.BG.Bar.Size = UDim2.new(1, 0, 1, 0)

Locked by sgsharp and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by
bobder2 135
10 years ago

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

01local maxthirst = 100 --create a max thirst value
02local currentthirst = 100 --create a currentthirst value
03 
04local currentthirstpercent = currentthirst/maxthirst --divide currentthirst by maxthirst to create a value between 0 and 1 to represent the percentage of thirst left.
05 
06function updatebar() --a function that will update the thirst bar. run this whenever you change the thirst
07    local currentthirstpercent = currentthirst/maxthirst --re-divides the thirst values
08    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
09end
10 
11function drink(amount) --example function for increasing thirst
12    currentthirst = currentthirst + amount --add thirst together
13    if currentthirst > maxthirst then --checks to see you aren't over the max
14        currentthirst = maxthirst --if you're over the max it sets you to the max
15    end
16    function updatebar() --updates the bar because you changed the value of thirst
17end

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.

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 SpazzMan502 133 — 10y
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. bobder2 135 — 10y
Ad