Tool not destroyed when clicked on a part

Hello!

I have made two scripts, a server and local.

In my serverscript, I scripted it so it detects when the player clicks on a part, then fires a remote event

In my local script, I scripted it so when it recieves the remote, it checks if the player is holding a tool and if so, it destroys the tool. However it does not destroy the tool.

Any solutions?
ServerScript:

local part = script.Parent
local ClickDet = script.Parent.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players")

ClickDet.MouseClick:Connect(function()
	ReplicatedStorage.SellRemote:FireClient(player)
end)

Local Script:

local Items = {
    ["Newspaper"] = 3,
    ["Rat"] = 2,
    ["Broken furniture"] = 70,
    ["Toy"] = 8,
    ["Bottle"] = 3,
    ["Shoes"] = 13,
    ["Monitor"] = 85,
    ["Clothes"] = 10,
    ["Scrap Metal"] = 2,
    ["Cardboard"] = 4,
    ["Book"] = 4,
	["Plastic Container"] = 7,
    ["Food Scraps"] = 13,
    ["Hammer"] = 30,
    ["Can"] = 3,
    ["Magazines"] = 12,
    ["Dishes"] = 27,
    ["Batteries"] = 25,
    ["Desk"] = 115,
    ["Chair"] = 43,
    ["Keyboard"] = 58,
    ["Lights"] = 43,
    ["PC"] = 468,
    ["Fridge"] = 324,
    ["Microwave"] = 286,
    ["Paint Brush"] = 11,
    ["Paint Can"] = 7
}

local player = game.Players.LocalPlayer
local RepStorage = game:GetService("ReplicatedStorage")
local SellRemote = RepStorage.SellRemote
local character = player.Character or player.CharacterAdded:Wait()

character.ChildAdded:Connect(function(newChild)
	if not newChild:IsA("Tool") then return end

	local value = Items[newChild.Name]
	if value then
		print(newChild.Name .. " is worth: " .. value)
	else
		print(newChild.Name .. " not found in table")
	end
	
	SellRemote.OnClientEvent:Connect(function() -- The function that I said
		if newChild.Equipped then
			newChild:Destroy()
			
		end
	end)
	
end)

Also if you’re wondering everything before the SellRemote function works fine.

1 Like

The issue I see here is that you’re using FireClient with the Players service named as player, you probably wanted to get player from the parameter of MouseClick!

By the way, you can connect to MouseClick from a LocalScript and there it will only fire when the LocalPlayer clicks upon the ClickDetector.

1 Like

When I equip and then unequip the tool, then it destroys the part. Any reason why that happens?

Is it because of this?

SellRemote.OnClientEvent:Connect(function() 
		if newChild.Equipped then
			newChild:Destroy()
			
		end
	end)

Tool.Equipped is not a boolean property of Tool that describes whether or not it’s currently equipped. It’s an event. The event fires whenever the tool is equipped. Events in Roblox are represented as RBXScriptSignal objects. Conditional statements in Luau interpret anything that is not false or nil as true. Values of these types are categorized as either “falsy” or “truthy”. Since RBXScriptSignal objects are not false or nil, Luau will pass the if-statement every time.

To check if a tool is currently equipped, you will need to check if its parent is that of its wielder’s character

What do you mean by wielder’s. And if you mean welded, how do you do that?

The player to whom the tool belongs

How do you check if its parent is that of the welders character.

Since you’re dealing with a LocalScript, is it safe to assume that newChild is a reference to a tool that the client owns? If so, get a reference to the client’s character via Players.LocalPlayer, then match the tool’s parent to that character

Can you say that in code form?

Do you not know your own script?

if newChild.Parent == character then
    -- ...
end

Like this?

SellRemote.OnClientEvent:Connect(function() 
		if newChild.Parent == game.Players.LocalPlayer.Character then
			print("Selling an item")
			newChild:Destroy()
		end
	end)

On another note, embedding an event listener to SellRemote within character.ChildAdded will cause a memory leak. I recommend moving your event listener outside of character.ChildAdded and refactoring it to:

SellRemote.OnClientEvent:Connect(function()
    local equippedTool = character:FindFirstChildOfClass("Tool")
    if equippedTool then
        equippedTool:Destroy()
    end
end)

The above code works more objectively; however, you should not have the client handle rewarding themselves with the value of the tool, as well as destroying the tool. The returned monetary value would not replicate, and exploiters could simply keep the tool after selling it

Here is the entire local script. Also, the part does not get destroyed when clicking on a part and equiiping the tool. The click part tool is in post 1 where I explain all of it.

local Items = {
    ["Newspaper"] = 3,
    ["Rat"] = 2,
    ["Broken furniture"] = 70,
    ["Toy"] = 8,
    ["Bottle"] = 3,
    ["Shoes"] = 13,
    ["Monitor"] = 85,
    ["Clothes"] = 10,
    ["Scrap Metal"] = 2,
    ["Cardboard"] = 4,
    ["Book"] = 4,
	["Plastic Container"] = 7,
    ["Food Scraps"] = 13,
    ["Hammer"] = 30,
    ["Can"] = 3,
    ["Magazines"] = 12,
    ["Dishes"] = 27,
    ["Batteries"] = 25,
    ["Desk"] = 115,
    ["Chair"] = 43,
    ["Keyboard"] = 58,
    ["Lights"] = 43,
    ["PC"] = 468,
    ["Fridge"] = 324,
    ["Microwave"] = 286,
    ["Paint Brush"] = 11,
    ["Paint Can"] = 7
}

local player = game.Players.LocalPlayer
local RepStorage = game:GetService("ReplicatedStorage")
local SellRemote = RepStorage.SellRemote
local character = player.Character or player.CharacterAdded:Wait()

character.ChildAdded:Connect(function(newChild)
	if not newChild:IsA("Tool") then return end

	local value = Items[newChild.Name]
	if value then
		print(newChild.Name .. " is worth: " .. value)
	else
		print(newChild.Name .. " not found in table")
	end
	
	SellRemote.OnClientEvent:Connect(function()
		local equippedTool = character:FindFirstChildOfClass("Tool")
		if equippedTool then
			equippedTool:Destroy()
		end
	end)
	
end)

I told you to move the SellRemote event handler outside of your character.ChildAdded handler

I have to go for a couple of hours. Will be able to talk to you after that!

I am back ready to work!!!