Hi,
I want make a health bar for npc using number values in it model.
I tryed this
script.Parent.Username.Text = script.Parent.Parent.Parent.Parent.Name
while true do
script.Parent.Frame.Size = UDim2.new(0,(script.Parent.Parent.Parent.Parent.health/script.Parent.Parent.Parent.Parent.maxhealth*200),1,0)
end
First of all, your while loop needs to yield, or it will freeze your game. For example:
while true do
wait(0.2) -- you can change the number or leave it empty, as "wait()"
-- code
end
I’d recommend you to use a function instead of a loop. Take a look at these articles on .Changed and .GetPropertyChangedSignal. With these, your code will only run when it should (not every t random number of seconds).
We would be more helpful if you provided more information, such as the output and the hierarchy. To open the output window, go to the menu on top of the screen in Studio > “View” tab > “Output”. Error messages will appear there. About the hierarchy, you could take a screenshot of the explorer, showing how the instances at issue are parented.
Now I assume that you are using two number values, “health” and “maxhealth”. These are objects, and you can’t divide objects. Get their Value property instead (health.Value and maxhealth.Value), which are numbers.
Important tip:
local something = script.Parent.Parent.Parent.Parent
You should store the object in a variable if you have to reference it frequently. Otherwise, a simple command will end up being a thousand kilometers long, making it really difficult to read. Also, the interpreter would take longer to index it.
I can see you’re somewhat new in scripting, but I’m sure you’ll learn fast!