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

How do I make a script where a GUI will show up when a tool is equipped?

Asked by 7 years ago
Edited by OldPalHappy 7 years ago

Hi! I've made a script where a GUI will be enabled when a tool is equipped. The LocalScript is inserted in a tool, as the GUI is in StartGui LocalScript:

local GUI = game.StarterGui.ScreenGui

if script.Parent.Equipped == true then
    GUI.Enabled = true
end

if script.Parent.Unequipped == false then
    GUI.Enabled = false
end

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

This is a Common Mistake

You actually want the PlayerGui, not the StarterGui service.

local plr = game.Players.LocalPlayer
local plrGui = plr:WaitForChild("PlayerGui")
local screenGui = plrGui:WaitForChild("ScreenGui")

if script.Parent.Equipped == true then
    screenGui.Enabled = true
end

if script.Parent.Unequipped == false then
    screenGui.Enabled = false
end

Next, this isn't connected to any event, so it'll only fire once, and never update. To fix this, lets use some events.

local plr = game.Players.LocalPlayer
local plrGui = plr:WaitForChild("PlayerGui")
local screenGui = plrGui:WaitForChild("ScreenGui")

local tool = script.Parent

tool.Equipped:Connect(function() -- when tool is equipped
    screenGui.Enabled = true
    tool.Unequipped:Wait() -- wait for tool to be uneqipped
    screenGui.Enabled = false
end)
Ad

Answer this question