How to get player who clicked the object?

So, i’m making a simple game (won’t reveal much about it) and i made bases for both blue and red teams, and since i’m pretty much noob at scripting, i started struggling.
Now what i’m struggling with:
So basically, i need to check which team player belongs to, that clicked the object, as i’m trying to make it, so if player from one team clicked it, it would damage the object (i made it humanoid), while if player from another team did the same, it would do nothing (or something that i’m not sure yet).
Anyways, thanks in advance for help.


if you are using a click detector to detect clicks then there is a player argument returned from the clickdetector.Click function

1 Like

If you used a ClickDetector then you can do something like


Also you can’t not “reveal” scripts, if you want help you need to show your script.


Thanks, but unfortunatelly there’s not much to reveal anyways, because i can’t wrap my head around how to phrase the whole script, as i mentioned that i have minimal understanding on scripting.


If you are trying to make game alone without any idea how to script this will be hard for you, i reccomend learning something before making games.


If you’re using the Teams system of Roblox, then you can look at player.Team or player.TeamColor.
I know this because the API reference says so:

And ClickDetector.MouseClick passes the player who clicked it:

The code would look something like this—

local clickdetector = script.Parent.ClickDetector
local humanoid = script.Parent.Humanoid -- you mentioned the object uses a Humanoid

-- the team object that cannot damage this. Has to refer to the actual Team in the Teams service
-- I use Teams["Red Team"] instead of Teams.Red Team because the name of the team has a space in it
local friendlyTeam = game:GetService("Teams")["Red Team"]

clickdetector.MouseClick:Connect(function(player) -- player is the player who clicked
	-- is the player a friend?
	if player.Team == friendlyTeam then -- player.Team is also the actual Team object
		-- do nothing
	else -- player is not a friend!

Change the variables and numbers here to fit your use case. Maybe your ClickDetector is somewhere else, or the Team is not named Red Team, or 10 damage is too much.
If you get an error, then try to understand what it means. Stuff like “index nil with Foo” usually means you’re doing nil.Foo or nil:Foo(), and the nil is coming from something you expected to exist, that didn’t actually exist.

Some ways to further improve this script:

  • Add a debounce or similar to make it take time to kill the object! Otherwise, an enemy may use an autoclicker to kill the object in less than a second.
  • friendlyTeam is “hardcoded” in the script, so if you want to change what team can’t harm it, then you have to change the script. You should put the name of the team in a StringValue or check the BrickColor of the object against the TeamColor of the player, so that the team can be changed without editing the script. It won’t help you much here, but it is a good habit.
  • Because the object has a Humanoid, it will probably also be harmed by most weapons.
  • Add some feedback to the clicks! Maybe make a ParticleEmitter :Emit() some particles when it takes damage, so the attacker knows they succeeded.
  • Connect something to the humanoid.Died event