Can't click Player

I have a script which is fired when the client clicks their left button on their mouse. I want it to fire when they click a player. Although I had the code:

local Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.Button1Down:Connect(function()
print(Mouse.Target)
end)

This would print Baseplate, but if I click on a player it prints nil. I was wondering how to fix this.

1 Like

If I hover over the Baseplate while I click it prints Baseplate so there’s no need for Mouse.Target.Name.

1 Like

That doesn’t work, if it’s nil then that would error.


@ClearlyJason nothing is wrong with the code, the further the player is the less accurate the mouse, I recommend you to make a custom raycasted mouse.

4 Likes

If there is no target, printing it’s name would result in an error.

1 Like

See the thing is I’m zoomed in on my own character. And click my own character and still nothing. Although other parts in workspace work?

1 Like

Ohhh, that’s because mouse.target always has the (local) character as a targetfilter no matter what

2 Likes

Is there a way to disable it? I’m trying to create a selection box around the character on click and then do some other stuff.

Local character. So It will be able to click other characters?

1 Like

Yeah. Just the current character can’t be used as a target

2 Likes

Is there a way to remove the local character from the filter?

1 Like

I don’t think so without using a custom raycast, heres just an example I stole borrowed from someone

local MAX_RAY_LENGTH = 5000
local mouse = game.Players.LocalPlayer:GetMouse()

function getMouseRay( rayLength )
	local rayLength = rayLength or 1
	return Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * rayLength)
end

function getMouseTarget(  )
	return game.Workspace:FindPartOnRay( getMouseRay(MAX_RAY_LENGTH) )
end

function getMouseTargetWithIgnoreList( ignorelist )
	return game.Workspace:FindPartOnRayWithIgnoreList( getMouseRay(MAX_RAY_LENGTH), ignorelist )
end

function getMouseTargetWithWhitelist( whitelist )
	return game.Workspace:FindPartOnRayWithWhiteList( getMouseRay(MAX_RAY_LENGTH), whitelist )
end

function getNonCanCollideParts(  )
	--[[Your choice of how to do this. You *could* build the list by iterating over every 
	instance in workspace each time, but that'd be pretty slow. My choice would be to use
	CollectionService and tag every non- CanCollide part when the game starts and when a 
	Part is added to Workspace. 

	E.g.:
	return TagS:GetTagged("NonCanCollide")
	]]
end

function getMouseTargetIgnoreNonCanCollide( )
	return getMouseTargetWithIgnoreList( getNonCanCollideParts() )
end

Use the getmousetarget function to get the mouse target

3 Likes

Your a life saver thank you so much! Works perfectly!

2 Likes