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 8 years ago
Edited by OldPalHappy 8 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:

1local GUI = game.StarterGui.ScreenGui
2 
3if script.Parent.Equipped == true then
4    GUI.Enabled = true
5end
6 
7if script.Parent.Unequipped == false then
8    GUI.Enabled = false
9end

1 answer

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

This is a Common Mistake

You actually want the PlayerGui, not the StarterGui service.

01local plr = game.Players.LocalPlayer
02local plrGui = plr:WaitForChild("PlayerGui")
03local screenGui = plrGui:WaitForChild("ScreenGui")
04 
05if script.Parent.Equipped == true then
06    screenGui.Enabled = true
07end
08 
09if script.Parent.Unequipped == false then
10    screenGui.Enabled = false
11end

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

01local plr = game.Players.LocalPlayer
02local plrGui = plr:WaitForChild("PlayerGui")
03local screenGui = plrGui:WaitForChild("ScreenGui")
04 
05local tool = script.Parent
06 
07tool.Equipped:Connect(function() -- when tool is equipped
08    screenGui.Enabled = true
09    tool.Unequipped:Wait() -- wait for tool to be uneqipped
10    screenGui.Enabled = false
11end)
Ad

Answer this question