This script is supposed to check if a player is on red team then if the player is on red team and touches a part then it will kill that player. but when I run the script and test it, it gives me this error: attempt to index nil with 'Team' - Server - RedTeamTouched:8
01 | local Team = game:GetService( "Teams" ) |
02 | local player = game.Players.LocalPlayer |
03 | local debounce = false |
04 | script.Parent.Touched:Connect( function (hit) |
05 | if player.Team.Name = = "Red Team" then |
06 | if debounce = = false then |
07 | debounce = true |
08 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
09 | hit.Parent.Humanoid:TakeDamage( 100 ) |
10 | end |
11 | wait( 1 ) |
12 | debounce = false |
13 | end |
14 | end |
15 | end ) wait( 1 ) |
16 | debounce = false |
17 | end |
18 | end |
19 | end ) |
I'm assuming you're using a server script (normal script). Unlike local scripts, Players.LocalPlayer
doesn't exist and will return nil
. If you want to get the Player
object from hit
(the limb of the player that touched script.Parent
), use Players:GetPlayerFromCharacter()
.
01 | local Players = game:GetService( "Players" ) |
02 | local Team = game:GetService( "Teams" ) |
03 |
04 | local debounce = false |
05 | script.Parent.Touched:Connect( function (hit) |
06 | local character = hit.Parent |
07 | local humanoid = character:FindFirstChildOfClass( "Humanoid" ) |
08 | local player = Players:GetPlayerFromCharacter(character) |
09 |
10 | if player then -- if `hit` is a limb of an actual player |
11 | if player.Team.Name = = "Red Team" then |
12 | if debounce = = false then |
13 | debounce = true |
14 | humanoid.Health = 0 -- instantly kills the player even with ForceField and/or infinite health |
15 | task.wait( 1 ) |
16 | debounce = false |
17 | end |
18 | end |
19 | end |
20 | end ) |
if it says that the team is nil
then it means that the player has no team assigned to it. Have you forgotten to assign the RedTeam to the player?
Also, you are trying to check if the player's team's Name is the "RedTeam" Instance. That doesnt work because names are strings, not objects/instances. Write this at line 8:
1 | if player.Team.Name = = "RedTeam" then |
Try switching the function name of Team to Teams and try what I've shown below. Also, what is the name of the team ingame? Does it have a space like Red Team or does it use just RedTeam?
01 | local Teams = game:GetService( "Teams" ) |
02 | local player = game.Players.LocalPlayer |
03 | local debounce = false |
04 | script.Parent.Touched:Connect( function (hit) |
05 | if player.Team = = Teams [ "Red Team" ] then |
06 | if debounce = = false then |
07 | debounce = true |
08 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
09 | hit.Parent.Humanoid:TakeDamage( 100 ) |
10 | end |
11 | wait( 1 ) |
12 | debounce = false |
13 | end |
14 | end |
15 | end ) |