I'm trying to get a tool, that when it is equipped it can click on a CoalRock and make it disappear. But I don't know how to check if it clicked on the CoalRock or anything.
✓ ANSWER ACCEPTED
How does one check if a tool clicked on a specific part?
1 answer
✓ Accepted answer
FIrst add RemoteEvent inside ReplicatedStorage and name it "Ore" after that add Localscript inside the tool:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Ore = ReplicatedStorage:WaitForChild("Ore")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse() -- We need to get mouse (only in local script)
local Tool = script.Parent
Tool.Activated:Connect(function() -- Runs function when you click while holding the tool
local Target = Mouse.Target -- Gets part that mouse is aiming at
if Target then -- check if it exists
if Target.Name == "CoalRock" then -- checks the name
local Distance = (Player.Character.HumanoidRootPart.Position-Target.Position).Magnitude
if Distance < 15 then -- Checks if plyer is close
Ore:FireServer(Target) -- fires to server
end
end
end
end)
after that add Script in ServerScriptServic and paste this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Ore = ReplicatedStorage:WaitForChild("Ore")
Ore.OnServerEvent:Connect(function(Player,Target) -- Receives event from client
local Distance = (Player.Character.HumanoidRootPart.Position-Target.Position).Magnitude
if Distance < 15 then -- checks 2nd time if player is close cause you should always check this on server
Target:Destroy() -- Destroys the target
end
end)
Note: Don't just copy and paste it, try to figure out what is happening, I made notes in the script
3 comments
0
It works! But what is the main point of the second script? Does it just check the distance and then destroys the object?
0
You need 2nd script cause if you destroy part with local script it would be visible to client only
0
Helped me too a lot.Thanks a lot I've been trying to figure this out for a long time.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Click or touch?