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

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

Asked by 7 years ago

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!

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. TheUniPiggy 77 — 7y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago

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
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? Brinceous 85 — 7y
0
You weren't clear when you said "the player will switch teams" cabbler 1942 — 7y
Ad

Answer this question