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

How do I make a gui appear when I hold a specific tool?

Asked by 4 years ago

I am making a tool that when held will cause a gui to appear in the lower right hand corner of the screen (by default, enabled is set to false). I am trying to use a local script to accomplish this, but I can't seem to get it to work. The script I have right now is down below.

Locations: The tool is a child of ServerStorage so that it can be pulled out using Adonis Admin commands. The script is a child of the tool, named "bait." The gui is located in StarterGui.

local player = game.Players.LocalPlayer
local tool = player.Character.bait -- The tool is named "bait"

if tool then
    player.PlayerGui.BaitLengthGui.Enabled = true
end

1 answer

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

Your variables are set up correctly, however using an if statement will not work due to the fact that it will only run once unless you loop it. Lucky for us, we have an event we can use to run something every time a tool is equipped. Lets connect it:

local player = game.Players.LocalPlayer
local tool = player.Character.bait

tool.Equipped:Connect(function()
--The lines of code in here will run every time this tool is equipped.
end)

Alright, now that we have the function connected we can display the gui.

local player = game.Players.LocalPlayer
local tool = player.Character.bait

tool.Equipped:Connect(function()
    player.PlayerGui.BaitLengthGui.Enabled = true
end)

I'm assuming that you want the gui to disappear when the tool is put away too. We lucked out once again as there is an event for when the player unequips a tool. Lets connect that in the same script:

local player = game.Players.LocalPlayer
local tool = player.Character.bait

tool.Equipped:Connect(function()
    player.PlayerGui.BaitLengthGui.Enabled = true
end)

tool.Unequipped:Connect(function()
    player.PlayerGui.BaitLengthGui.Enabled = false
end)

Your tool should now be working.

Hope I helped!

1
Thank you! It didn't work at first but I realized that I needed to change "local tool = player.Character.bait" to "local tool = player.Character:WaitForChild("bait")" because the tool starts in the backpack. Thanks for your help! ZeroNoey 19 — 4y
Ad

Answer this question