Im trying to see if a users mouse is on a user problem is I don't know how to know if the part the mouse is on is part of the user here is what I am trying
local player = game.Players.LocalPlayer local mouse = player:GetMouse() function find_parent(item) for _,v in pairs(game.Players:GetChildren()) do local success, response = pcall(function() local catch = item while true do if catch.Parent.Name == v.Name then return true end catch = catch.Parent wait() end end) if success then return response else return false end end end mouse.Move:connect(function() local target = workspace:FindPartOnRay( Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction*50000) ) if target then if find_parent(target) then print('yes') end end end)
this code will work if the mouse is hovering over the persons legs but otherwise will just not print yes is there a better way todo this?
thanks, ira
Your code is iterating over the children of Players, which are the Player objects, not the players' character models which are children of the Workspace. Normally, to detect if a part is inside a character model (and whose) you'd so something like this:
local Players = game:GetService("Players") ... local model = part:FindFirstAncestorOfClass("Model") if model and model~=game.Workspace then local player = Players:GetPlayerFromCharacter(model) if player then -- part is inside the character of this player end end
Roblox character Models do not have other Models inside of them normally, so this will work. If your game puts Models inside players' character Models, parts of those models will not return you a player, you'd need to keep searching up the ancestry chain recursively until you reach the Workspace (which is a Model) or get nil for some reason (could happen if you yielded and player was removing).