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

My script gives me an error. Why does it not work? (Check bottom for more info)

Asked by 1 year ago
Edited 1 year ago

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

01local Team = game:GetService("Teams")
02local player = game.Players.LocalPlayer
03local debounce = false
04script.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
15end)        wait(1)
16            debounce = false
17        end
18    end
19end)

3 answers

Log in to vote
0
Answered by 1 year ago

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().

01local Players = game:GetService("Players")
02local Team = game:GetService("Teams")
03 
04local debounce = false
05script.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
20end)
0
lol you answer all my questions MariamOMG090 9 — 1y
Ad
Log in to vote
-1
Answered by
Klui36 45
1 year ago
Edited 1 year ago

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:

1if player.Team.Name == "RedTeam" then
0
it still does not work, it gives me the same error MariamOMG090 9 — 1y
0
check if the player has a team assigned to it... Klui36 45 — 1y
Log in to vote
-1
Answered by 1 year ago

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?

01local Teams = game:GetService("Teams")
02local player = game.Players.LocalPlayer
03local debounce = false
04script.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
15end)   

Answer this question