Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

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.

0
In what part of your code are you struggling? Post it alongside your question please Le_Teapots 913 — 6y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
6 years ago

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

0
There’s something wrong with the indent systems, please don’t mind the messy script Ziffixture 6913 — 6y
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! FierceLeviathan 10 — 6y
0
Sure, I’d be glad to learn something new! Ziffixture 6913 — 6y
0
I'll post it as an answer to the question FierceLeviathan 10 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
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)

0
Interesting, I never knew that was a form factor, that changes everything on my terms, thanks! Ziffixture 6913 — 6y

Answer this question