Hello, I am learning to use the mouse, I already know how to use the getmouse but now I am doing tests with the mouse.Target and the truth has been a bit complicated for me, the target takes the position of the mouse or what exactly is it used for? and how can I get the mouse position?
Sorry for the inconvenience … I share the code that I am testing and learning from the docs
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.Random()
end
end)
end)
Hi, mouse.Target returns an object in workspace that mouse is currently hovering, U wonder a use of it? For example you want to create a tool that would colour certain parts by just a click on it
That is incorrect, doing mouse.Hit.p would return the position the mouse is at in the 3D world which the position might not be the exact position of a part, that’s why you could use mouse.Target to get the object the mouse is hovering over. Keep in mind that it will return invisible parts.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = Instance.new("Part") -- You can just change this to part.Parent like you were doing before.
part.Parent = workspace
part.Anchored = true
mouse.TargetFilter = part -- The mouse will ignore the part so it doesn't end up above the mouse position
mouse.Button1Down:connect(function()
part.Position = mouse.HIt.p -- mouse.Hit.p is a Vector3 already
-- mouse.HIt.p is equivalent to Vector3.new(mouse.Hit.p.X,mouse.Hit.p.Y,mouse.Hit.p.Z)
end)
With this script what I do is that when I click on a part of the map the part moves to that position, but how can I make the part move constantly to the mouse position while I move the mouse?
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = Instance.new("Part") -- You can just change this to part.Parent like you were doing before.
part.Parent = workspace
part.Anchored = true
mouse.TargetFilter = part -- The mouse will ignore the part so it doesn't end up above the mouse position
mouse.Button1Down:connect(function()
part.Position = mouse.HIt.p -- mouse.Hit.p is a Vector3 already
-- mouse.HIt.p is equivalent to Vector3.new(mouse.Hit.p.X,mouse.Hit.p.Y,mouse.Hit.p.Z)
part.CFrame = CFrame.new(mouse.Hit.p) --Setting the CFrame of the part to the mouses position in the 3D world (without the orientation)
end)
Yes that was just showing how to move the part to the mouse. If you want to constantly move it then you can wrap that in a RenderStepped or whatever like @nuttela_for1me said.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = Instance.new("Part") -- You can just change this to part.Parent like you were doing before.
part.Parent = workspace
part.Anchored = true
mouse.TargetFilter = part -- The mouse will ignore the part so it doesn't end up above the mouse position
mouse.Button1Down:connect(function()
-- mouse.Hit.p is a Vector3 already
-- mouse.HIt.p is equivalent to Vector3.new(mouse.Hit.p.X,mouse.Hit.p.Y,mouse.Hit.p.Z)
local RunService = game:GetService("RunService")
local RATE_PER_SECOND = 2
RunService.RenderStepped:Connect(function(step)
local increment = RATE_PER_SECOND * step
increment = part.Position
end)
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = Instance.new("Part") -- You can just change this to part.Parent like you were doing before.
part.Parent = workspace
part.Anchored = true
mouse.TargetFilter = part -- The mouse will ignore the part so it doesn't end up above the mouse position
mouse.Button1Down:connect(function()
-- mouse.Hit.p is a Vector3 already
-- mouse.HIt.p is equivalent to Vector3.new(mouse.Hit.p.X,mouse.Hit.p.Y,mouse.Hit.p.Z)
local RunService = game:GetService("RunService")
local RATE_PER_SECOND = 2
RunService.RenderStepped:Connect(function(step)
local increment = RATE_PER_SECOND * step
part.Position = mouse.Hit.p
mouse.Hit.p = increment
end)
end)