Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detect what part in the workspace a player clicked on with a tool equipped?

Hi. I am wondering how would you detect a part that is in the workspace with a tool equipped, and return that value to the player's gui? Thanks!

EDIT: I don't really know where to start, and would like some guidance. The player will be using a pickaxe. If the object the player's mouse is on, is an ore, then the game will let that ore get 'mined 'by the specific player. I am asking for the returned value to the player's gui since I want it to update based on what ore they are looking at, and how full the progress bar is.

1 comment

0

In what part of your code are you struggling? Post it alongside your question please

2 answers

✓ Accepted answer

This request with all will be a lot, I will only help you get started, this should be handled in the LocalScript aiding the use of the ‘Pickaxe’, for this to work we’re going to use mouse.Target, This returns whatever object the cursor is beneath.

I will be Scripting by the default structure of a Tool: Tool - Handle - LocalScript, remember that when Scripting a Tool, primarily just creating one, a BasePart of the name, with a capital H, “Handle”.

local tool = script:FindFirstAncestor(“Pickaxe”)
local handle = tool:WaitForChild(“Handle”)
local equipped = false

local player = game:GetService(“Players”).LocalPlayer
local cursor = player:GetMouse() --// I prefer “cursor”
local cursorPos = cursor.Target

local oreGui = player:FindFirstChild(‘PlayerGui”).oreGui

tool.Equipped:Connect(function()
   if not equipped then return end
   equipped = true
   if (oreGui) then
      spawn(function()
         while equipped wait(.5) do
            cursorPos = Cursor.Target
         end
      end); wait()
      while equipped wait(.5) do
         pcall(function()
            local cursorObject = cursorPos
            if (cursorObject:IsA(“BasePart”) and cursorObject.Name == “Ore”) then
               oreGui.Text = cursorObject.Name
            end
         end
      end
   end
end)

This should get you started, creating this kind of cursorCheck, one I’m familiar with, is quite hard, but this will get you up on your feet, it has flaws but is functional, it’s up to you to finish it, I’m not going to write something that long, you understand, I hope this helps you out anyway, if so, don’t forget to accept this answer and upvote! Good luck

4 comments

0

There’s something wrong with the indent systems, please don’t mind the messy script

0

Thank you so much for the guidance. I successfully got it to work! I used a different function from yours, but it essentially does the same thing, but in a super small form factor. I can send it to you to check it if you want!

0

Sure, I’d be glad to learn something new!

0

I'll post it as an answer to the question

local tool = script.Parent
local handle = tool.Handle
local equipped = false

local player = game.Players.LocalPlayer
local cursor = player:GetMouse()
local cursorPos = cursor.Target

local gui = player:WaitForChild("PlayerGui"):WaitForChild("OreGui").TextLabel

tool.Equipped:Connect(function()
	cursor.Move:Connect(function(mouse)
		cursorPos = cursor.Target
		pcall(function()
			local cursorObject = cursorPos
			if cursorObject.Name == "BaseOreRockTest" then
				gui.Text = cursorObject.Name
			end
		end)
		
	end)
	
end)

1 comment

0

Interesting, I never knew that was a form factor, that changes everything on my terms, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.