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

Need help trying to get a part follow a player's mouse. Any Suggestions? {SOLVED}

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!

2 comments

0

Do not use player:GetMouse for new work, it is being superseded by uis

0

hey Astrosupernuat, not sure if you saw the updated post, just tellin 'ya here so you get notified :)

1 answer

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)

9 comments

0

I ran this code through modifying my existing local script above and it is quite interesting. It does what i want it to do, BUT the highlighter part just starts zooming into the camera repetitively, quite weird haha. The cause of that is perhaps the method of assigning the highlighter through mouse.Hit.Position, cuz instead i went with CFrame but somehow nothing works. Thanks for the insights tho.

0

(mouse) will eventually be deprecated, use user input service instead. for your problem, you are only changing the position once, you must constantly set its position for it to work

0

Do not use player:GetMouse for new work. It is being superseded by uis

0

Astrosupernuat, the part is zooming into your camera because the CanQuery property is set to true. I should’ve highlighted that in my post to disable those properties.

0

Ioowa_yawn, i did utilize the canQuery but thing is that it no longer wants to follow the mouse and it just disappears. I tried using the user input service and it does work with no errors but it still doesnt follow my mouse in the game. I need an explanation and solution to this problem because i tried nearly every solution i can think of atm. Im using CFrame but idk if i should use it.

0

oof, thats terrible. i have one last trick i can try. i have updated my answer, tell me if you have any luck :)

0

Hey Ioowa, so far no errors, which is pretty nova, but it still comes out as the same thing, where the highlighter doesnt move with the mouse.

0

Btw, are the comments on the edit telling me to put the variables i have in the function? or is it a description of what it does. It might just be how my script is designed cuz its all under an equpped event and an if statement ensuring the mouse targets the baseplate before firing the code block.

0

also i updated the script by editing the original post so you can see where im at :)

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