Local function onClicked(player) doesn't work

I am making a system where you place down the tool after pressing a surface and then I want to pick it back up. To do so I compare the Attribute between the tool in lighting and the one in workspace, if the Attribute matches to the one in lighting which is also in a folder then it will copy the tool into the player who clicked inventory.

The problem is when I press the placed item it doesn’t activate the script, but for some reason when I drag only the “handle” into workspace and try then it works, which doesn’t make any sense since the system I use clones the handle into the workspace.

script inside ClickDetector

local clickDetector = script.Parent
local handle = script.Parent
local clickDetector = handle:FindFirstChildOfClass("ClickDetector")

local function onClicked(player)
	local ID = script.Parent:GetAttribute("ID")
		local lightingTool = game:GetService("Lighting"):FindFirstChild("Food"):FindFirstChildWhichIsA("Tool")
			if lightingTool:GetAttribute("ID") == ID then
				lightingTool:Clone(player.PlayerName.Backpack)
			end
end

clickDetector.MouseClick:Connect(onClicked)

The system I use in the localscript

local clickDetector = script.Parent
local handle = script.Parent
local clickDetector = handle:FindFirstChildOfClass("ClickDetector")

local function onClicked(player)
	local ID = script.Parent:GetAttribute("ID")
		local lightingTool = game:GetService("Lighting"):FindFirstChild("Food"):FindFirstChildWhichIsA("Tool")
			if lightingTool:GetAttribute("ID") == ID then
				lightingTool:Clone(player.PlayerName.Backpack)
			end
end

clickDetector.MouseClick:Connect(onClicked)

image

image

Hi,

I noticed in both of your scripts you assign the same “clickDetector” variable twice.

local clickDetector = script.Parent -- Once
local handle = script.Parent
local clickDetector = handle:FindFirstChildOfClass("ClickDetector") -- Twice?

Inside the localscript, you’re gonna have to say:

local handle = script.Parent:WaitForChild("Handle")
local clickDetector = handle.ClickDetector

Also

if lightingTool:GetAttribute("ID") == ID then
	lightingTool:Clone(player.PlayerName.Backpack)
end

You can’t clone an object like that. This is how I would do it:

local clone = lightingTool:Clone()
clone.Parent = player.Backpack

Try implementing the tips I have given you, and tell me if it worked.

Good luck!

Doesn’t work still, but it works only if I drag the handle into the workspace

No, hold on I’m a bit delusional

Yes, I did fix that. But the only problem is that it doesn’t work after used the place system and tried to pick it up again, it only works if I drag the tools handle into workspace myself.

Hello.

First off, let’s use a print statement to check that the function is actually being called. You can place the print statement varying locations in the “onClicked” function, although I would prefer if you placed it at the start of the function.

I placed the print statement in varying locations and it doesn’t work after the onClicked function.

I see your problem.

local handle = script.Parent.Handle; -- replace handle with this

That wouldn’t be the issue as the handle isn’t in the click detector

How come you have two declarations of the same instance?

local clickDetector = script.Parent
local handle = script.Parent

If you’re strictly looking for the handle, it would be “script.Parent.Parent” in the handle variable.

local handle = script.Parent.Parent
local clickDetector = handle.ClickDetector

local function onClicked(player)
	local lightingTool = game:GetService("Lighting"):FindFirstChild("Food"):FindFirstChildWhichIsA("Tool")
	if lightingTool:GetAttribute("ID") == handle:GetAttribute("ID") then
		local clone = lightingTool:Clone()
		clone.Parent = player.Backpack
		handle:Destroy()
	end
end

clickDetector.MouseClick:Connect(onClicked)

This is what the code is. It wasn’t updated

Here is the issue I’m facing, for some odd reason it works when I drag handle into workspace myself instead of the script doing it for me.