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

Is there a way to change a frame's size based on leaderstats?

P = script.Parent.Parent.Parent.Parent
Front = script.Parent
P.leaderstats.MaxEXP.Value = (P.leaderstats.Level * 59)
while true do 

wait()
if P.leaderstats.EXP == 0 then
	Front.Size.X = 0
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 10)
	then
	Front.Size.X = .1
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 20)
	then
	Front.Size.X = .2
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 30)
	then
	Front.Size.X = .3
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 40)
	then
	Front.Size.X = .4
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 50)
	then
	Front.Size.X = .5
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 60)
	then
	Front.Size.X = .6
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 70)
	then
	Front.Size.X = .7
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 80)
	then
	Front.Size.X = .8
else if
	P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 90)
	then
	Front.Size.X = .9
else if
	P.leaderstats.EXP > (P.leaderstats.MaxEXP)
	then
	Front.Size.X = 1 and
	P.leaderstats.LastLevel.Value + 1 and
	P.leaderstats.EXP.Value == 0
	
end
end 

1 answer

✓ Accepted answer

You can't say Front.Size.X = 1 because X is readonly. You have to recreate the UDim2:

Front.Size = UDim2.new(0, 1, 0, Front.Size.Y.Offset) --assumes that Front.Size.X.Scale and Y.Scale are both 0. If not, you'll need to access those values as well.

Also, don't use "and" to join statements together, as you've done on lines 48 to 50.

You can make further improvements to your script:

Use a Changed event instead of a while loop (less processing over time):

P.leaderstats.EXP.Changed:connect(function()
	if P.leaderstats.EXP > (P.leaderstats.MaxEXP) then
		P.leaderstats.LastLevel.Value = P.leaderstats.LastLevel.Value + 1 --are you sure you don't want to say "P.leaderstats.Level.Value"?
		P.leaderstats.EXP.Value = 0
		--You probably will want to update MaxEXP here, too, like so:
		P.leaderstats.MaxEXP.Value = P.leaderstats.Level.Value * 59
	end
	--modify Front.Size based on EXP here (see below)
end)

Use a formula instead of a bunch of if statements. In your expression

P.leaderstats.EXP > (P.leaderstats.Level * 59 / 100 * 50)

note that the changing value is the * 50 at the end. We can simplify the ending part to * 59 * 0.5. This is convenient because we want Front.Size.X to range from 0 to 1. Now we can replace the final part, the "* 0.5", with a variable: "* width". Now we solve for width:

P.leaderstats.EXP = (P.leaderstats.Level * 59 * width)

width = P.leaderstats.EXP / P.leaderstats.Level / 59

and now we can use 'width' when resizing Front. This part of the script will now look like this:

--at the end of the Changed function
local width = P.leaderstats.EXP / P.leaderstats.Level / 59
Front.Size = UDim2.new(0, width, 0, Front.Size.Y.Offset)

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