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)
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.