Can't Click a Click Detector While Holding a Tool (I already did research)

I realize many topics similar to this have been solved many times, but those are all at least a year old and/or do not work for my purposes.

I am trying to destroy a union when the player clicks it and has a certain tool in hand. Here is the script:

local clickDetector = script.Parent.ClickDetector
local spill = script.Parent
local clicks = 0
local addPoints = require(game:GetService("ServerStorage").AddPoints)

clickDetector.MouseClick:Connect(function(plr)
	print("spill clicked")
	local backpack = plr:WaitForChild("Backpack")
	local character = plr.Character or plr.CharacterAdded:Wait()
	
	local mop = backpack.Mop or character.Mop
	if not mop then
		return nil
	end
	print("mop found")
	if mop.Parent == character then
		addPoints.addPoints(plr)
		spill:Destroy()
	end
end)

I couldn’t do this by checking if the mouse is pointing at the union when the tool is activated because every 90 seconds, the union is replicated with the same name each time. I apologize if there has been a solution recently that would work for my case, but I didn’t find it. I wanted to see if there has been an update allowing click detectors to be activated while holding a tool and/or how to fix this.

1 Like

You can just use UserInputService with InputEnded and Mouse.Target

--Client script
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local remote = game:GetService('ReplicatedStorage').mop
game:GetService('UserInputService').InputEnded:Connect(function(input:InputObject)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		remote:FireServer(mouse.Target)
	end
end)
--Server script
local clicks = 0
local addPoints = require(game:GetService("ServerStorage").AddPoints)
local remote = Instance.new('RemoteEvent')
remote.Name = 'mop'
remote.Parent = game:GetService('ReplicatedStorage')

remote.OnClientEvent:Connect(function(plr,part)
	print("spill clicked")
	local backpack = plr:WaitForChild("Backpack")
	local character = plr.Character or plr.CharacterAdded:Wait()

	local mop = backpack.Mop or character.Mop
	if not mop then
		return nil
	end
	print("mop found")
	if mop.Parent == character and part.Name == 'spill' then
		addPoints.addPoints(plr)
		part:Destroy()
	end
end)

Make sure your spill parts are named spill or else they won’t be destroyed. (This code is untested)

2 Likes

There is an error saying that Mop is not a valid member of backpack. Is this a problem with saying that mop is either backpack.Mop or character.Mop?

I figured it out. I just said that mop was character.Mop, because if its in the backpack we don’t want it to do anything anyways. I also got replaced mop.Parent == character with mop and I deleted the backpack variable because it is not needed. Here is the final code on the server:

local fireCleaning = game:GetService("ReplicatedStorage").FireCleaning
local clicks = 0
local addPoints = require(game:GetService("ServerStorage").AddPoints)

fireCleaning.OnServerEvent:Connect(function(plr,part)
	print("mop used")
	local character = plr.Character or plr.CharacterAdded:Wait()
	
	local mop = character.Mop	
	if not mop then
		return nil
	end
	
	if mop and part == script.Parent then
		part:Destroy()
		addPoints.addPoints(plr)
	end
end)