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
local Team = game:GetService("Teams") local player = game.Players.LocalPlayer local debounce = false script.Parent.Touched:Connect(function(hit) if player.Team.Name == "Red Team" then if debounce == false then debounce = true if hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid:TakeDamage(100) end wait(1) debounce = false end end end) wait(1) debounce = false end end 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()
.
local Players = game:GetService("Players") local Team = game:GetService("Teams") local debounce = false script.Parent.Touched:Connect(function(hit) local character = hit.Parent local humanoid = character:FindFirstChildOfClass("Humanoid") local player = Players:GetPlayerFromCharacter(character) if player then -- if `hit` is a limb of an actual player if player.Team.Name == "Red Team" then if debounce == false then debounce = true humanoid.Health = 0 -- instantly kills the player even with ForceField and/or infinite health task.wait(1) debounce = false end end end 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:
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?
local Teams = game:GetService("Teams") local player = game.Players.LocalPlayer local debounce = false script.Parent.Touched:Connect(function(hit) if player.Team == Teams["Red Team"] then if debounce == false then debounce = true if hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid:TakeDamage(100) end wait(1) debounce = false end end end)