How would I get where a player clicks when they're using a tool?

Hello, I’m wondering how I could detect where a player clicks while they’re using a tool. What I want to achieve is something similar to Grow a Garden’s seed placement system where they get the position of where the player clicks checks if it’s valid and places something where the player clicked.

1 Like

Hey! You can try detecting when player clicks with User Input Service, and then check if the tool is inside of Player.Character

1 Like

Like this:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local character = player.Character or player.CharacterAdded:Wait()
		if character then
			local toolEquipped= nil
			for _, obj in pairs(character:GetChildren()) do
				if obj:IsA("Tool") and obj.Name == "Your tool name" then
					toolEquipped = obj
					break
				end
			end

			if toolEquipped then
               ...
			end
		end
	end
end)

I hope it helped you!

If you also want to check mouse hit CFrame, it would need some more lines

You can also do another method, similar to as mentioned, except I feel its a whole lot less complicated as you dont need to check if the tool is equipped or not.

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local Tool = 'YourToolHere'

Tool.Activated:Connect(function() -- // This runs when you click!
	local MouseHitPosition, MouseTarget = Mouse.Hit.Position, Mouse.Target
	
	print(MouseHitPosition) -- This is the position the mouse is at in the 3D space, aka where the mouse hit
	print(MouseTarget) -- this is the object (e.g. Baseplate, Spawnpad, part, etc.) that the mouse is over top of
end)
1 Like
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local Mouse = player:GetMouse()

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local MousePosition = Mouse.Hit.Position
		local character = player.Character or player.CharacterAdded:Wait()
		if character then
			local toolEquipped= nil
			for _, obj in pairs(character:GetChildren()) do
				if obj:IsA("Tool") and obj.Name == "Your tool name" then
					toolEquipped = obj
					break
				end
			end

			if toolEquipped then
               ...
			end
		end
	end
end)
1 Like

Yeah, this is a better way! It would make it simple!

1 Like

Quick question, where would this script go?

I believe this only works in the client (or inside of a local script, which should be parented anywhere that a local script can run (e.g. StarterCharacterScripts, inside of the Tool, etc.)

1 Like

Put it in the tool and gave this error message: Workspace.1vgot_terminated.Tier 1 Seed.Place:5: attempt to index nil with ‘Connect’ - Client - Place:5

You have to replace the Tool variable with the actual tool.

In this case where the script is inside of the tool, just set the Tool value to this:

Tool = script.Parent
1 Like

Thank you so much! I thought it was the name of the tool. Thanks for helping!

2 Likes

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