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

Tween Not working when adding this ScreenGui?

so ive got a tween im doing and it works fine until i add an XP bar to the script

let me explain where things are, ive got 2 ScreenGui within StarterGui 1 names "HUD" and 1 named "XPBar"

this script is placed under "HUD" within an ImageButtoncalled Left

so when i try to add "Frame" which is under "XPBAR" the tween button stops working all together, im pretty noob at lua so im sure im just doing it wrong....here is my script

Bar = script.Parent.Parent.Frame --Placed in HUD
Hotbar = script.Parent.Parent.HotBar --Placed in HUD
Left = script.Parent --Placed in HUD
Right = script.Parent.Parent.Right --Placed in HUD
Xp = script.Parent.Parent.Parent.XPBar.Frame --Placed in XPBar. Probably did this part wrong :P Shaddup im learning :C
Open=true

function RunGui()
	if Open then
	Bar : TweenPosition(UDim2.new(0, -30, 0, 0) + Bar.Position)
	Hotbar : TweenPosition(UDim2.new(0, -30, 0, 0) + Hotbar.Position)
	Left : TweenPosition(UDim2.new(0, -30, 0, 0) + Left.Position)
	Right : TweenPosition(UDim2.new(0, -30, 0, 0) + Right.Position)
	Xp : TweenPosition(UDim2.new(0, -30, 0, 0) + Xp.Position)
	end
end
script.Parent.MouseButton1Down:connect(RunGui)

1 answer

✓ Accepted answer

Your script is attempting to set variables to objects that aren't there yet. The variables are being set right when the server begins. During that time, the instances within the game are still loading (which could include XPBar).

To wait for the children appear, you can use the :WaitForChild() method. The parameter for that event is the object that you are waiting to appear.

You do not need to use :WaitForChild() on the other variables because they are all parents of this script, and parents of objects are always created before their children (like this script).

Fixed Code:

Bar = script.Parent.Parent.Frame
Hotbar = script.Parent.Parent.HotBar
Left = script.Parent
Right = script.Parent.Parent.Right
Xp = script.Parent.Parent.Parent:WaitForChild("XPBar"):WaitForChild("Frame") --You have to wait for the instances to appear in the game.
Open=true

function RunGui()
    if Open then
    Bar : TweenPosition(UDim2.new(0, -30, 0, 0) + Bar.Position)
    Hotbar : TweenPosition(UDim2.new(0, -30, 0, 0) + Hotbar.Position)
    Left : TweenPosition(UDim2.new(0, -30, 0, 0) + Left.Position)
    Right : TweenPosition(UDim2.new(0, -30, 0, 0) + Right.Position)
    Xp : TweenPosition(UDim2.new(0, -30, 0, 0) + Xp.Position)
    end
end
script.Parent.MouseButton1Down:connect(RunGui)

Here is more on the method WaitForChild.

If I helped you out, be sure to accept my answer!

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