When tool equipped and click on a part, destroys a model

Hello!

I have made a script where if the tool (it is called MasterKey) is equipped and when the player clicks on a part (called DoorPart) , it destroys a model (Called Door) My issue is that when equipping the tool and clicking on the Door Part, I want it to destroy the Door. Any Solutions?

Script:

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local DoorPart = workspace.Build.DoorPart
local Door = workspace.Build.Door


mouse.Button1Down:Connect(function()
	if mouse.Target == DoorPart then
		Door:Destroy()
	end
end)

Explorer:
image_2024-12-24_172723680

You are asking for solutions, but never state the issue. I’m assuming the photo you sent is from the LocalScript inside Build. LocalScripts inside the workspace don’t run, as that is a server based environment. Move the script to your tool, and check if the tool is equipped before destroying the door.

I have moved the local script to the tool and changed the script to put the parent as the tool. My issue is that when equipping the tool and clicking on the Door Part, I want it to destroy the Door.

Script:

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local DoorPart = workspace.Build.DoorPart
local Door = workspace.Build.Door
script.Parent = localPlayer.Backpack:WaitForChild("MasterKey")


mouse.Button1Down:Connect(function()
	if mouse.Target == DoorPart then
		Door:Destroy()
	end
end)

Add some print statements around and send more photos of your Explorer.

My PC was glitched, so sorry it took a while.

Pictures:
Screenshot 2024-12-24 173838
Screenshot 2024-12-24 173826

Did you add the print statements? Put something like this in between major blocks to ensure all the code is running:

print(“Test1”)

You should be able to find what line is the issue.

1 Like

Two things: you only want to destroy the door itself, not the door frame:

change local Door = workspace.Build.Door
to local Door = workspace.Build.Door:WaitForChild("Door")

and now to check if the player is clicking on the door, you actually want to see if they are clicking on a descendant of the door model:

change

if mouse.Target == DoorPart then
    Door:Destroy()
end

to

if mouse.Target:IsDescendantOf(Door) then
	Door:Destroy()
end

one thing to keep in mind while using this method is that deleting objects with LocalScripts is CLIENT SIDED. This means that only the person who unlocks the door will be able to walk through it, the deletion will not be replicated to the server. You should look at the other post and see if ManualActivationOnly helps you with that setup

I do also have to say your setup is a little confusing. You should not need that additional DoorPart with a ClickDetector if you are checking the mouse target

2 Likes

I got help from another guy. But thanks for helping and Merry Christmas!