How to detect if player is holding a specific thing in their inventory

Im trying to make a button visible but only if the player is holding the specific tool that is in their inventory, Please give me a simple script idea on how to detect if a player is holding the tool :slight_smile: please help <3

EDIT: this is my updated script and it works except that the button doesnt stop showing when i stop holding the tool:

local Tool = script.Parent

Tool.Equipped:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Tool.Parent)
if player then
local playerGui = player:FindFirstChildOfClass(“PlayerGui”)
if playerGui then
playerGui:WaitForChild(“ScreenGui”):WaitForChild(“ToolBtns”):WaitForChild(“Wave1Btn”).Visible = true
end
end
end)

Tool.Unequipped:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Tool.Parent)
if player then
local playerGui = player:FindFirstChildOfClass(“PlayerGui”)
if playerGui then
playerGui:WaitForChild(“ScreenGui”):WaitForChild(“ToolBtns”):WaitForChild(“Wave1Btn”).Visible = false
end
end
end)

1 Like

When a player equips a tool, it gets parented to their character.
You can check whether a specific tool is equipped by doing something like:

if Player.Character:FindFirstChild("ToolName") then
    -- Tool is equipped
end

Another method is by using the .Equipped event.

local Tool=sript.Parent
Tool.Equipped:Connect(function()
     -- Make GUI visible
end)
Tool.Unequipped:Connect(function()
     -- Make GUI hidden
end)
2 Likes

Local script or script? and where do i put it

Ideally, for your case, use a LocalScript inside the tool to detect when the tool is equipped and unequipped.

1 Like

okay it didnt work tho … this is my script:

local Tool = script.Parent

Tool.Equipped:Connect(function()
game.StarterGui.ScreenGui.ToolBtns.Wave1Btn.Visible = true
end)
Tool.Unequipped:Connect(function()
game.StarterGui.ScreenGui.ToolBtns.Wave1Btn.Visible = false
end)

When a player joins the game, all elements in the StarterGui get cloned
into something called the PlayerGui in the player.

Instead of using game.StarterGui.ScreenGui
you will need to use game.Players.LocalPlayer.PlayerGui.ScreenGui

1 Like

Ok but it still won’t work, here is the script:

local Tool = script.Parent

Tool.Equipped:Connect(function()
game.Players.LocalPlayer.PlayerGui.ToolBtns.Wave1Btn.Visible = true
end)
Tool.Unequipped:Connect(function()
game.Players.LocalPlayer.PlayerGui.ToolBtns.Wave1Btn.Visible = false
end)

you missed adding ScreenGui after PlayerGui.

PlayerGui.ScreenGui.ToolBtns

not, PlayerGui.ToolBtns

2 Likes

works now thank u !!! :slight_smile: <3

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.