Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to see if a part is part of a user?

Asked by 4 years ago

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

0
bruh youre calling a function with an infinite while loop every pixel the mouse moves... not good. Psudar 882 — 4y
0
yes, but the loop will only run 2 or 3 times max i really can't think of any better way to do this. iranathan 7 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
thank you that is exactly what i needed not only did you fix my problem you also made the code way shorter. iranathan 7 — 4y
Ad

Answer this question