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
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!