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

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)

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

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)
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:

if 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?

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)    

Answer this question