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

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

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
the :connect is not problem, problem is the GUI you need to edit GUI in PlayerGui. yHasteeD 1819 — 5y
0
oh :/ HappyTimIsHim 652 — 5y
0
Edited HappyTimIsHim 652 — 5y
0
You forgot to found the GUI. yHasteeD 1819 — 5y
View all comments (4 more)
0
So why does his script not work? Please explain why it is not working not just posting a working script itself. Warfaresh0t 414 — 5y
0
are you by chance using this in a server script? Server Scripts cannot access the PlayerGui Despayr 505 — 5y
0
its in starterpack inside a tool HappyTimIsHim 652 — 5y
0
if this has no Handle then turn off requirehandle HappyTimIsHim 652 — 5y

Answer this question