Adding a Click Detecter if holding an item

Hi, so I am trying to make a game where players have to find eggs around the map. I want to make it so that players can only collect eggs if they are holding the easter basket I have put for the players to pick up. I need to know how to make this script only work if the player is holding the basket

local part = script.Parent
local detector = part.ClickDetector

detector.MouseClick:Connect(function(player)
	if player.PlayerGui.EggCounter.Frame.TextBox2.Text == "0/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "1/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "1/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "2/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "2/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "3/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "3/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "4/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "4/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "5/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "5/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "6/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "6/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "7/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "7/8" then
		player.PlayerGui.EggCounter.Frame.TextBox2.Text = "8/8"

	elseif player.PlayerGui.EggCounter.Frame.TextBox2.Text == "8/8" then
		player.PlayerGui.EggCounter.Frame.Visible = false
		player.PlayerGui.Congrats.Frame.Visible = true
	end
end)

Any help would be greatly appreciated.
Thank You.

on a server script, you would make a BoolValue inside the player using Instance.new()
and name it IsHoldingBasket.
After that, you’d make a script inside the tool and do:

Tool.Equipped:Connect(function()
    Player.IsHoldingBasket = true
end)

Tool.Unequipped:Connect(function()
    Player.IsHoldingBasket = false
end)
1 Like

Ok, can you show how I would write the server script?

local part = script.Parent
local detector = part:WaitForChild('ClickDetector')

local function onDetectorClick(player)
	-- cancel script if the player doesn't have any tools equipped
	if not player.Character:FindFirstChildWhichIsA('Tool') then return end
	
	-- create a variable for the textbox
	local textBox = player.PlayerGui.EggCounter.Frame.TextBox2.Text
	
	-- create a variable for both the eggcounter and congrats frame
	local eggCounter = player.PlayerGui.EggCounter
	local congrats = player.PlayerGui.Congrats
	
	if textBox == "0/8" then
		textBox = "1/8"
	elseif textBox == "1/8" then
		textBox = "2/8"
	elseif textBox == "2/8" then
		textBox = "3/8"
	elseif textBox == "3/8" then
		textBox = "4/8"
	elseif textBox == "4/8" then
		textBox = "5/8"
	elseif textBox == "5/8" then
		textBox = "6/8"
	elseif textBox == "6/8" then
		textBox = "7/8"
	elseif textBox == "7/8" then
		textBox = "8/8"
	elseif textBox == "8/8" then
		eggCounter.Frame.Visible = false
		congrats.Frame.Visible = true
	end
end

detector.MouseClick:Connect(onDetectorClick)

for a specific tool:

local part = script.Parent
local detector = part:WaitForChild('ClickDetector')

local function onDetectorClick(player)
	-- cancel script if the player doesn't have a tool equipped
	if not player.Character:FindFirstChild('TOOL NAME') then return end
	
	-- create a variable for the textbox
	local textBox = player.PlayerGui.EggCounter.Frame.TextBox2.Text
	
	-- create a variable for both the eggcounter and congrats frame
	local eggCounter = player.PlayerGui.EggCounter
	local congrats = player.PlayerGui.Congrats
	
	if textBox == "0/8" then
		textBox = "1/8"
	elseif textBox == "1/8" then
		textBox = "2/8"
	elseif textBox == "2/8" then
		textBox = "3/8"
	elseif textBox == "3/8" then
		textBox = "4/8"
	elseif textBox == "4/8" then
		textBox = "5/8"
	elseif textBox == "5/8" then
		textBox = "6/8"
	elseif textBox == "6/8" then
		textBox = "7/8"
	elseif textBox == "7/8" then
		textBox = "8/8"
	elseif textBox == "8/8" then
		eggCounter.Frame.Visible = false
		congrats.Frame.Visible = true
	end
end

detector.MouseClick:Connect(onDetectorClick)

I used the script for the specific tool and nothing is happening. There aren’t any errors in the output either.

you changed TOOL NAME right?

also im not that familiar with findfirstchild so see if this one works

local part = script.Parent
local detector = part:WaitForChild('ClickDetector')

local function onDetectorClick(player)
	if player.Character:FindFirstChild('TOOL NAME') then 
		-- create a variable for the textbox
		local textBox = player.PlayerGui.EggCounter.Frame.TextBox2.Text

		-- create a variable for both the eggcounter and congrats frame
		local eggCounter = player.PlayerGui.EggCounter
		local congrats = player.PlayerGui.Congrats

		if textBox == "0/8" then
			textBox = "1/8"
		elseif textBox == "1/8" then
			textBox = "2/8"
		elseif textBox == "2/8" then
			textBox = "3/8"
		elseif textBox == "3/8" then
			textBox = "4/8"
		elseif textBox == "4/8" then
			textBox = "5/8"
		elseif textBox == "5/8" then
			textBox = "6/8"
		elseif textBox == "6/8" then
			textBox = "7/8"
		elseif textBox == "7/8" then
			textBox = "8/8"
		elseif textBox == "8/8" then
			eggCounter.Frame.Visible = false
			congrats.Frame.Visible = true
		end
	end
end

detector.MouseClick:Connect(onDetectorClick)
--  Create a normal script and insert it on ServerScriptService
game.Players.PlayerAdded:Connect(function(player)
    local isHoldingBasket = Instance.new("BoolValue", player)
    isHoldingBasket.Name = "IsHoldingBasket"
end)

You shouldn’t make if statements that long, you can add a number (like the example below)

local EggNumber = 0 -- Or you can create an intvalue directly in the player
local function onDetectorClick(player)
	if player.Character:FindFirstChild('TOOL NAME') then 
		-- create a variable for the textbox
		local textBox = player.PlayerGui.EggCounter.Frame.TextBox2.Text
                EggNumber += 1
                textBox = EggNumber.."/8"
                if EggNumber == 8 then
                  eggCounter.Frame.Visible = false
	              congrats.Frame.Visible = true
            end
      end
end

Yes, I did change TOOL NAME to the name of the tool I want it to work with, but it still doesn’t do anything and no errors.

Are you making the player only collect the eggs locally (on the client) or on the server?

I tried your script and nothing happens with no erros in the output.

I am using a normal script. Should I try with a local script?

I wanted to say when you’re holding a tool, I don’t think it will work with click detector. The script upward was the only function that you connect when the player click the eggs.

1 Like

It depends, you want the player collect only on their game or on everyone’s game?

Wouldn’t it be able to work if its in your backpak?

oh yeah it works when its in your backpack, but it won’t work if you’re holding it. By the way I need you to answer my question so that I can give you the best solution.

I would like the players to collect it only on their game so they cant steal eggs from others

Then the script must be a local script. Also I read your post again, I suppose you need to use mouse functions if you want to make the player only collect eggs while holding basket.

You should try reading and understanding this post before carrying on your script.
https://developer.roblox.com/en-us/api-reference/class/Mouse

srry for not responding if u want to do it with it in the players backpack you would replace the line with

if player.Character:FindFirstChild('TOOL NAME') or player.Backpack:FindFirstChild('TOOL NAME') then 

tbh i would just use proximity prompt at this point because its a lot more modern and you can still make it clickable if u want