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

How to edit a Frame's size?

I have a localscript inside a Frame, in a ScreenGUI inside the player's playerGUI. It is supposed to make the frame's size the player's health. I have it like follows:

-- Part 1
local player = game.Players.LocalPlayer --The local player.
	    local humanoid = player.Character:WaitForChild("Humanoid") --Yields (makes the script wait) until the apples object becomes available.
	    script.Parent.Size =  {0, math.floor(humanoid.Health)},{0, 5} 
	    humanoid.Changed:connect(function() --Connects a function to an event. Known as an anonymous function (as it has no direct identifier.)
	        script.Parent.Size = {0, math.floor(humanoid.Health)},{0, 5}  --Sets the Text property of the script's parent to "Apples: " then the apples variable's value. The two dots are used for string concatenation (merging of values to form a string.)
	    -- Part 1 doesn't work
		-- Part 2
			if humanoid.Health >= 75 then
				script.Parent.BackgroundColor3.r=(0/255)  
				script.Parent.BackgroundColor3.g=(170/255)
				script.Parent.BackgroundColor3.b=(127/255)
			end
			elseif humanoid.Health >= 50 and humanoid.Health <= 74 then
				script.Parent.BackgroundColor3.r=(47/255)  
				script.Parent.BackgroundColor3.g=(170/255)
				script.Parent.BackgroundColor3.b=(61/255)
			end
			elseif humanoid.Health >= 25 and humanoid.Health <= 49 then
				script.Parent.BackgroundColor3.r=(170/255)  
				script.Parent.BackgroundColor3.g=(157/255)
				script.Parent.BackgroundColor3.b=(82/255)
			end
			elseif humanoid.Health >= 0 and humanoid.Health <= 24 then
				script.Parent.BackgroundColor3.r=(170/255)  
				script.Parent.BackgroundColor3.g=(80/255)
				script.Parent.BackgroundColor3.b=(80/255)
			end
		-- Haven't even tested part 2 but the first elseif is underlined in red saying: expected enf next to function line 4, got 'elseif' 
end)

Thanks.

This is the new script: (It only does the colour change once, and the Size once, then doesn't work)

local player = game.Players.LocalPlayer --The local player.
	        local humanoid = player.Character:WaitForChild("Humanoid")
	        script.Parent.Size = UDim2.new( 0, math.floor(humanoid.Health),0, 5)
	        humanoid.Changed:connect(function()
	            script.Parent.Size = UDim2.new(0, math.floor(humanoid.Health),0, 5)
	            if humanoid.Health >= 75 then
	                script.Parent.BackgroundColor3 = Color3.new(0/255, 170/255, 127/255)
	            elseif humanoid.Health >= 50 and humanoid.Health <= 74 then
	                script.Parent.BackgroundColor3.Color3 = Color3.new(47/255, 170/255, 61/255)
	            elseif humanoid.Health >= 25 and humanoid.Health <= 49 then
	                script.Parent.BackgroundColor3 = Color3.new(170/255, 157/255, 82/255)
	            elseif humanoid.Health >= 0 and humanoid.Health <= 24 then
	                script.Parent.BackgroundColor3 = Color3.new(170/255, 80/255, 80/255)
	            end
	end)

1 answer

That's because you did if,end,elseif,end... NUUH!!! It should be if, elseif, else, end. Also, you forgot the UDim2.new() after the = for each rezise command. I think this should work now.

local player = game.Players.LocalPlayer --The local player.
        local humanoid = player.Character:WaitForChild("Humanoid")
        script.Parent.Size = UDim2.new( 0, math.floor(humanoid.Health),0, 5)
        humanoid.Changed:connect(function()
            script.Parent.Size = Udim2.new(0, math.floor(humanoid.Health),0, 5)
            if humanoid.Health >= 75 then
                script.Parent.BackgroundColor3.r=(0/255) 
                script.Parent.BackgroundColor3.g=(170/255)
                script.Parent.BackgroundColor3.b=(127/255)
            elseif humanoid.Health >= 50 and humanoid.Health <= 74 then
                script.Parent.BackgroundColor3.r=(47/255) 
                script.Parent.BackgroundColor3.g=(170/255)
                script.Parent.BackgroundColor3.b=(61/255)
            elseif humanoid.Health >= 25 and humanoid.Health <= 49 then
                script.Parent.BackgroundColor3.r=(170/255) 
                script.Parent.BackgroundColor3.g=(157/255)
                script.Parent.BackgroundColor3.b=(82/255)
            elseif humanoid.Health >= 0 and humanoid.Health <= 24 then
                script.Parent.BackgroundColor3.r=(170/255) 
                script.Parent.BackgroundColor3.g=(80/255)
                script.Parent.BackgroundColor3.b=(80/255)
            end
end)

4 comments

1

Bear in mind you can't set the r, g and b properties of a Color3, they are read only. You have to do "script.Parent.BackgroundColor3 = Color3.new(r,g,b)" with r, g and b being the colour values.

0

Ah, thanks.

0

Edited script.

0

Does it work after Spongocardo's fixes? Also, I apologize for not realizing that mistake...

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