Help with where player clicked with a tool

  1. Basically i’m trying to detect which part player clicked on whilst holding a tool

  2. I’ve tried a couple of ways however I cant seem to find the solution, It has to be serverside, not just client

for context I want to place a part on a part which the player has clicked on with a tool

You can get the player’s mouse in a local script and use remote events to tell the server where to place the part.

Create a remote event in ReplicatedStorage called “PlacePart”

-- LOCAL SCRIPT INSIDE TOOL
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse() -- Gets the player's mouse
local tool = script.Parent
local remoteEvent = game:GetService('ReplicatedStorage'):WaitForChild("PlacePart")

function onActivate() -- Function to run when the player uses the tool
	remoteEvent:FireServer(mouse.Hit.Position) -- Gives the server the player's targetted part and mouse position
end

tool.Activated:Connect(onActivate)
-- SCRIPT INSIDE SERVERSCRIPTSERVICE
local remoteEvent = game:GetService('ReplicatedStorage'):WaitForChild("PlacePart")

function onEvent(player, position) -- stores the information send from the client in 2 variables
	local part = Instance.new("Part") -- creates the part
	part.Position = position -- moves the part to the position send from the client
	part.Parent = workspace -- puts the part in the workspace
end

remoteEvent.OnServerEvent:Connect(onEvent)

If you have any questions or errors feel free to ask me

Yeah, this works as you intended it but I want it to only be able to place on a specific part and not anywhere in the world

-- LOCAL SCRIPT INSIDE TOOL
function onActivate()
	if mouse.Target.Name == "Part" then -- Change this if statement to whatever you're checking for
		remoteEvent:FireServer(mouse.Hit.Position)
	end
end

mouse.Target is the part the player is looking at

1 Like

Thanks a lot for the help!!! :heart:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.