Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Tween Not working when adding this ScreenGui?

Asked by
nicros 165
8 years ago

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

Log in to vote
1
Answered by
Discern 1007 Moderation Voter
8 years ago

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!

Ad

Answer this question