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.
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