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 9 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

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)

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
9 years ago

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.

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 — 9y
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 — 9y
Ad