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

Why isn't my GUI that runs when the tool is equipped executing?

Hello, I have created a little segment of code that I have put in a LocalScript, which I then put into a Tool, which then I put into StarterPack. Upon running the code, I get the numbers (my personal way of ensuring that the code is being run) but the TextBox is not being created. A solution would help. Thanks.

local Potion = script.Parent
local GUI = game.StarterGui.ScreenGui

local function Selected()
	local TxtBox = Instance.new("TextBox", GUI)
	TxtBox.Position = UDim2.new(0.5,0,0.5,0)
	TxtBox.Size = UDim2.new(0.5,0,0.5,0)
	TxtBox.BackgroundColor3 = Color3.new(0,0,0)
	print("1")
end

local function Deselected()
	local TxtBox = GUI:WaitForChild("TextBox")
	TxtBox:Destroy()
	print("2")
end
Potion.Equipped:connect(Selected)
Potion.Unequipped:connect(Deselected)

2 answers

✓ Accepted answer

Decided to answer this because the last answer was unclear and didn't even put the real directory of the GUI...

On line 2 you put:

local GUI = game.StarterGui.ScreenGui

This is technically right, however it's not the actual player's Gui. The StarterGui service contains everything that is cloned into a thing called PlayerGui inside the player. The PlayerGui is what the player sees, not the StarterGui. So you were changing the Gui that is cloned into the player, meaning if you reset after you selected the tool it would have probably changed it on your new Gui.

We have to find the player's PlayerGui, and since we are uising a local script we can do game.Players.LocalPlayer and it will get the local player without needing the name. Then we will get PlayerGui which is a child of the player, and then finally your Gui called ScreenGui.

local GUI = game.Players.LocalPlayer.PlayerGui:FindFirstChild("ScreenGui")

Final Script:

local Potion = script.Parent
local GUI = game.Players.LocalPlayer.PlayerGui:FindFirstChild("ScreenGui")

local function Selected()
    local TxtBox = Instance.new("TextBox") -- Removed the Instance Parent
    TxtBox.Parent = GUI -- Added parent code
    TxtBox.Position = UDim2.new(0.5,0,0.5,0)
    TxtBox.Size = UDim2.new(0.5,0,0.5,0)
    TxtBox.BackgroundColor3 = Color3.new(0,0,0)
    print("1")
end

local function Deselected()
    local TxtBox = GUI:WaitForChild("TextBox")
    TxtBox:Destroy()
    print("2")
end
Potion.Equipped:Connect(Selected) -- Changed :connect to :Connect
Potion.Unequipped:Connect(Deselected) -- Changed :connect to :Connect

In addition I've included changes to your script to make it all up to date. :connect is deprecated for :Connect and Instance's Parent argument is deprecated. Commented at the end of the lines I changed.

This is a very common error

local Potion = script.Parent
local GUI = game.Players.LocalPlayer:WaitForChild("PlayerGui")

local function Selected()
    local TxtBox = Instance.new("TextBox", GUI)
    TxtBox.Position = UDim2.new(0.5,0,0.5,0)
    TxtBox.Size = UDim2.new(0.5,0,0.5,0)
    TxtBox.BackgroundColor3 = Color3.new(0,0,0)
    print("1")
end

local function Deselected()
    local TxtBox = GUI:WaitForChild("TextBox")
    TxtBox:Destroy()
    print("2")
end
Potion.Equipped:Connect(Selected)
Potion.Unequipped:Connect(Deselected)

I hope you learn from this :connect() is deprecated so u need to use :Connect(). Also this has to be in a localscript.

8 comments

0

the :connect is not problem, problem is the GUI you need to edit GUI in PlayerGui.

0

oh :/

0

Edited

0

You forgot to found the GUI.

0

So why does his script not work? Please explain why it is not working not just posting a working script itself.

0

are you by chance using this in a server script? Server Scripts cannot access the PlayerGui

0

its in starterpack inside a tool

0

if this has no Handle then turn off requirehandle

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