Search code examples
luaroblox

Roblox How to detect a player's team


I am making a Roblox game, and I need to detect a player's team somehow. My code currently looks like this:

script.Parent.Touched:Connect(function(part)
    local plr = part.Parent.Name
    if (game.Players:FindFirstChild(plr).Team == "Police") then
        ....
    end
end)

And when I touch that part (it's an invisible wall), it gives me an error: Workspace.Part.Script:3: attempt to index a nil value

What am I doing wrong?

Edit: I found out it can't find my name in game.Players, because now I tried:

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:FindFirstChild(hit.Parent.Name)
    if (plr.Team == "Police") then
...

And now I get Workspace.Part.Script:3: attempt to index local 'plr' (a nil value)

Edit2: Now I tried printing plr (game.Player:FindFirstChild(hit.Parent.Name)) and it was ' Miniller', not 'Miniller', and now I didn't get any errors, but the code below also did nothing..


Solution

  • I solved it by not using variables AND not "Police", it's game.Teams.Police, so code:

    if (game.Players:FindFirstChild(hit.Parent.Name).Team = game.Teams.Police
    ...