I’m trying to make a GUI appear when a tool is equipped, but it’s not working. Here’s the Lua script I’m using:
local stick = game.Workspace.Tool
local function picked(tool)
local player = game.Players:GetPlayerFromCharacter(tool.Parent)
if player then
print("The object was collected")
local gui = Instance.new("ScreenGui")
local text = Instance.new("TextLabel")
local playerGui = player:WaitForChild("PlayerGui")
text.Size = UDim2.new(1, 0, 1, 0)
text.BackgroundColor3 = Color3.new(1, 1, 1)
text.Parent = gui
gui.Parent = playerGui
else
print("Unable to find the player for the character")
end
end
stick.Equipped:Connect(picked)
The goal is to create a ScreenGui with a TextLabel and parent it to the PlayerGui when a player equips a tool. I’ve checked that the GetPlayerFromCharacter function is returning the correct player, and I’ve verified that the ScreenGui and TextLabel are being created. However, the GUI doesn’t appear on the player’s screen.
Any help or suggestions on what might be causing the issue would be greatly appreciated. Thank you!
You’d be better off cloning an already existing GUI as Instance.new is a pretty heavy task. However if you absolutely have to, I think your problem lies how you are not configuring the GUI properly. I’m not 100% sure, but using Instance.new automatically has Enabled turned off on default. Hope this helps!
local stick = workspace.Tool
local function picked(mouse) --Your 'tool' variable is getting the player's mouse, not a tool instance.
local player = game.Players:GetPlayerFromCharacter(stick.Parent) -- I'm assuming that you want the player that has the tool "Stick" in their hands.
if player then
print("The object was collected")
local gui = Instance.new("ScreenGui")
local text = Instance.new("TextLabel")
local playerGui = player:WaitForChild("PlayerGui")
text.Size = UDim2.new(1, 0, 1, 0)
text.BackgroundColor3 = Color3.new(1, 1, 1)
text.Parent = gui
gui.Parent = playerGui
else
print("Unable to find the player for the character")
end
end
stick.Equipped:Connect(picked)