Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I get the parent of an object a player is looking at when the mouse is clicked?

I'm trying to make a tool that when equipped and clicked on a player, the player will switch teams. So far, I've looked into the API:Class/Tool section of the wiki, but nothing to be found there. I then checked out The API:Class/PlayerMouse and saw the 'BasePart Target' property, 'GetNetworkOwner()' function, and 'IsA()' function. I've thought about using the BasePart property to find the parent of the target, then see if humanoid belonged to the target, but I haven't seen anything about the parent of a target in the properties category of the API:Class/BasePart page. I was thinking about doing a similar thing with the IsA() function, by checking if the object that was clicked on is a part, getting the parent, seeing if there was a humanoid, then continuing with the code. I'm not exactly clear on what the GetNetworkOwner function does, as there isn't much on it in the wiki, but maybe using that to get the parent of the object clicked on as well? I was hoping to receive some enlightenment as to how I would go about getting the player/checking if it was a player that the player holding the tool clicked on, and how I would go about using the functions. Thank you for any and all help!

1 comment

0

Check what the mouse clicked, then get the parent of the object. Im not quite sure on this because I haven't delved into it and I'm making a quick assumption.

1 answer

✓ Accepted answer

GetNetworkOwner is irrelevant. What you want to do is really easy.

mouse.Target is the part the player is looking at. Once you know this, you can check to see if the target is of a character.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:connect(function()
    --mouse clicked
    local part = mouse.Target --part the mouse is pointing at
    if part then
        local char = part.Parent
        local player= game.Players:GetPlayerFromCharacter(char)

        if player then
            --found player from part
            game.Players.LocalPlayer.Team = player.Team
        end
    end
end

2 comments

0

Why did you use localplayer? Wouldn't that change the team of the player using the tool rather than the player being targeted by it?

0

You weren't clear when you said "the player will switch teams"

This discussion is preserved as it was. New answers, comments, and votes are closed.