"GunUI is not a valid member of PlayerGui" error when GunUI is a valid player of it

Hi! I’m trying to tween Frame under GunUI from a tool, but I’m getting the error GunUI is not a valid member of PlayerGui, I’ve tried changing it to StarterGui but then the Tween doesn’t work. This is issuing from a LocalScript.

Here’s my script:

local tool = script.Parent
local plr = game:GetService("Players").LocalPlayer
local PlayerGui = game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')
local part = PlayerGui.GunUI.Frame
part.Position = UDim2.new(-0.2, 0, 0.75, 0)

local goal1 = {}
goal1.Position = UDim2.new(0, 0, 0.75, 0)

local goal2 = {}
goal2.Position = UDim2.new(-0.2, 0, 0.75, 0)

local tweenInfo1 = TweenInfo.new(1)
local tween1 = TweenService:Create(part, tweenInfo1, goal1)

local tweenInfo2 = TweenInfo.new(1)
local tween2 = TweenService:Create(part, tweenInfo2, goal2)

tool.Equipped:Connect(function() 
	tween2:Cancel()
	tween1:Play()
end)

tool.Unequipped:Connect(function()
	tween2:Play()
	tween1:Cancel()
end)

The reason it cancels the other tween is because if players spam the tool, I don’t want the tweens to break.
Any help is appreciated!

1 Like

Since PlayerGui is already the natural descendants of the Player, you don’t have to use WaitForChild() on them.

Your GunUI is the one that needs :WaitForChild() because it is being loaded from StarterGui or any other parent.

The problem is that the gui didn’t load yet when you’ve declared the variable.
local part = PlayerGui.GunUI.Frame => local part = PlayerGui:WaitForChild("GunUI").Frame
Usually if this is done on the client, it happens that the local scripts, execute before the entire game loads, all I would suggest is to use WaitForChild() when declaring an object variable in your code, since the client might not have a good computer that would be strong enough to load the game that quick.
Good luck! and let me know if it worked :slight_smile:

1 Like