Hey, so I’m having an issue using the Target property of the player’s mouse. It appears that no matter where I point my mouse, it’ll never detect that it’s aiming at a limb of my character. I just print whatever the mouse’ target is, and it seems to just go right through my character.
Also tried setting the TargetFilter to nil, can’t figure out why this might be happening…
local localPlayer = game.Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local camera = workspace.CurrentCamera
local lastPlayer
local UIEnabled
local length = 125 -- distance to scan
game["Run Service"].RenderStepped:Connect(function()
local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
local target = workspace:FindPartOnRay(ray, localPlayer.Character)
if target and target.Parent and target.Parent:FindFirstChild("Humanoid") then
local playerTarget = game.Players:GetPlayerFromCharacter(target.Parent) -- checks if target is a player
if playerTarget then --playerTarget if you want the mouse to scan a Player. Change to Target if you want it to scan anything with a Humanoid in it.
print(target.Name)
--code you want
end
end
end)
The Mouse object is very deprecated, but you can use workspace:FindPartOnRay to find a part from the mouse’s UnitRay property:
local RunService = game:GetService("RunService")
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
-- Run this function every frame
RunService.RenderStepped:Connect(function()
-- Save mouse.UnitRay to a variable
local mouseRay = mouse.UnitRay
-- Make the ray longer
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
-- Pass ray to workspace:FindPartOnRay
local p = workspace:FindPartOnRay(castRay)
print(p)
end)
The new services that have superceded the Mouse object is UserInputService, as well as ContextActionService to the same extent.
An example of each in practice:
local UserInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
-- Variables used to save the mouse's position on the screen
local x, y = 0, 0
-- Connection to UIS's InputChanged event
UserInputService.InputChanged:Connect(function(input, processed)
-- Checking for the right UserInputType
if input.UserInputType == Enum.UserInputType.MouseMovement then
-- Saving the input's position to x and y
x, y = input.Position.X, input.Position.Y
end
end)
local ContextActionService = game:GetService("ContextActionService")
local x, y = 0, 0
-- Binding an action using CAS's BindAction function
ContextActionService:BindAction("raycasting",function(name, state, input)
-- Checking for the right UserInputState
if state == Enum.UserInputState.Change then
-- Saving the input's position to x and y
x, y = input.Position.X, input.Position.Y
end
-- Binding the action to the mouse's movement
end,false,Enum.UserInputType.MouseMovement)
-- Run this function every frame
RunService.RenderStepped:Connect(function()
-- Creating the ray from x and y, passed into camera:ScreenPointToRay
local mouseRay = camera:ScreenPointToRay(x, y)
-- Make the ray longer
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
-- Pass ray to workspace:FindPartOnRay
local p = workspace:FindPartOnRay(castRay)
print(p)
end)
Most curious though, I believe the only way to easily access what object the mouse is looking at is with the Target property, which supposedly is not deprecated, which would make sense to there being nothing wrong with it?
I don’t get it. Might also be worth noting this was tested with R16 rigs.
The character is automatically ignored by the mouse, even if you set a TargetFilter. You will need to extend the mouse’s UnitRay to accomplish this using one of the above methods.