Hey guys, I need help trying to get this simple part that highlights a specific area of the ground so that way you can plant seeds in that area, as i'm developing a farming game as a first project. Trouble is that i cannot find a way to simply have the part follow the mouse's position at all times as long as the tool is equipped.
Here's the Code:
local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() local Seeds = script.Parent local mousePosX = mouse.X local mousePosY = mouse.Y Seeds.Equipped:Connect(function(player) if mouse.Target == game.Workspace.Baseplate then local Highlighter = game.ReplicatedStorage.Highlighter:Clone() Highlighter.Parent = game.Workspace Highlighter.Anchored = true Highlighter.Transparency = 0.5 Highlighter.Material = "Neon" local highlighterPos = Highlighter.Position Highlighter.CFrame = CFrame.new(highlighterPos) + Vector3.new(mousePosX, mousePosY) end end) Seeds.Unequipped:Connect(function() local world = game.Workspace if world:FindFirstChild("Highlighter") then local Highlighter = game.Workspace.Highlighter Highlighter:Destroy() end end)
I know there's one thing thats missing but i just dont know what it is exactly. Lemme know how you approach this problem. Thanks!
EDIT Heres the latest script as it is right now.
local Player = game.Players.LocalPlayer local userinputService = game:GetService("UserInputService") local Seeds = script.Parent local mouse = Player:GetMouse() Seeds.Equipped:Connect(function(player) if mouse.Target == game.Workspace.Baseplate then local Highlighter = game.ReplicatedStorage.Highlighter:Clone() ---Highlighter.Parent = game.Workspace Highlighter.Anchored = true Highlighter.Transparency = 0.5 Highlighter.Material = "Neon" local function CalculateMousePosition() local Camera = workspace.CurrentCamera local Position = userinputService:GetMouseLocation() local unitRay = Camera:ScreenPointToRay(Position.X, Position.Y + 36, 0) local rayCal = workspace:Raycast(unitRay.Origin, unitRay.Direction * 100) return rayCal end end end) Seeds.Unequipped:Connect(function() local world = game.Workspace if world:FindFirstChild("Highlighter") then local Highlighter = game.Workspace.Highlighter Highlighter:Destroy() end end)
EDIT 2 I found out the solution through the Roblox DevForum and all i had to do was involve the TargetFilter and associate the part.Position with mouse.Hit.Position and add a Vector3.new and now it's functioning PERFECTLY!!!
Thx guys for helping out!
edit:
I've just made a function in which it returns a RaycastResult for getting the mouse's 3D position data, feel free to use it and test it out:
local UserInputService = game:GetService('UserInputService') local function CalculateMousePosition() local Camera = workspace.CurrentCamera local Position = UserInputService:GetMouseLocation() local unitRay = Camera:ScreenPointToRay(Position.X, Position.Y + 36, 0) local rayCal = workspace:Raycast(unitRay.Origin, unitRay.Direction * 100) -- returns a table with variables: -- Position <Vector3> -- Instance <any | basepart> -- Distance <number> -- Normal <Vector3> return rayCal end
-- archive? --
A method I use (im not sure if it's good or not) is using the mouse's Hit.Position value to get the mouse's current 3D position in the world
(see the Engine API mouse documentation)
-- variables local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() -- highlighter behaviour -- MouseHighlighter is a Part object -- this Part has all CanCollide, CanTouch and CanQuery properties disabled (false) local Highlighter = script.MouseHighlighter:Clone() local HighlighterActive = false local function StartHighlight() Highlighter.Parent = workspace HighlighterActive = true repeat task.wait() -- set the position of the part to the mouse's 3d position Highlighter.Position = mouse.Hit.Position until HighlighterActive == false end local function EndHighlighter() Highlighter.Parent = script HighlighterActive = false end -- testing code local UserInputService = game:GetService('UserInputService') UserInputService.InputBegan:Connect(function(input, GPE) if GPE then return end if input.KeyCode == Enum.KeyCode.E then if HighlighterActive then EndHighlighter() else StartHighlight() end end end)
this script is placed inside of StarterPlayerScripts
(fyi i very crudely made this script, use it as a proof of concept .. there's probably a better way of doing this that saves one extra rendering frame)