Want to destroy union when clicked [Time Sensitive]

I want to destroy a union when the player clicks it if the tool is clicked. 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)
	local backpack = plr:WaitForChild("Backpack")
	local character = plr.Character or plr.CharacterAdded:Wait()
	
	local mop = backpack.Mop
	if not mop then
		return nil
	end

	if mop.Parent == character then
		addPoints.addPoints(plr)
		spill:Destroy()
	end
end)

The problem is, when I click the union, it doesn’t call the function. How can I fix this?

The module script is located in ServerStorage, move it to ServerScriptService because Scrips don’t run in ServerStorage. Then in the code change local addPoints = require(game:GetService("ServerStorage").AddPoints) to local addPoints = require(game:GetService("ServerScriptService").AddPoints).

In-Correct, module scripts can be placed in server storage and required from anywhere as long as it’s on the server

1 Like

Ah alright then, sorry about that.

It might be calling the function, however it might be getting stopped because it might have not found the mop. If the mop is being currently equipped it is already in the character and not the backpack

1 Like

I agree, equipped tools are on the character model, otherwise they sit in the backpack.

You assume Mop is parented to backpack then check if its parent is the character.
The only way this destroys is if two mutually exclusive scenarios exist at same time.

To double check by adding a print statement inside the function before you check for mop, this will tell you if the clicker/calling is successful.

Try

local mop = character:FindFirstChild("Mop")
if mop then
   addPoints.addPoints(plr)
   spill:Desroy()
end
1 Like

After doing some debugging, I found that it is able to get the variables before the function, but it won’t run the function.

Print mop and the children of the players backpack before you check if it exists

I put a print statement at the top of the function like this:

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 = character:FindFirstChild("Mop")
	
	if mop then
		addPoints.addPoints(plr)
		spill:Destroy()
	end
end)

The statement didn’t run.

Print the ClickDetector variable

It printed the ClickDetector. Do you think the issue could be that I’m holding a tool when I click the union?

Yes, that is exactly what I said before:

Try clicking the union when your not holding the tool and see if that works, if it does then I’ll help you make it work when you hold it aswell

1 Like

Oh, oops. Guess my brain isn’t working today lol. Sorry about the confusion. Ok, so I just tested and the function does run correctly. Now I just need help to make it work when the mop is equipped.

Ok great, try this:


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

clickDetector.MouseClick:Connect(function(plr)
	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

	if mop.Parent == character then
		addPoints.addPoints(plr)
		spill:Destroy()
	end
end)
1 Like

Now the same issue is happening. Should I change the mop to manual activation only?