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

How to get rid of this local player nil error?

Asked by 4 years ago
Edited 4 years ago

I keep having the following error pop up:

20:08:48.348 - Workspace.TimerSystem.FinishWall.Script:142: attempt to index local 'player' (a nil value)

Here is my script

-- Initialization
local TimerSystem = script.Parent.Parent
local Board = TimerSystem:WaitForChild("Board")
local SurfaceGui = Board:WaitForChild("SurfaceGui")
local Clock = SurfaceGui:WaitForChild("TextLabel")

-- Local Variables
local FinishWall = script.Parent
local FinishedFolder = FinishWall:WaitForChild("FinishedFolder")
local Leaderboard = TimerSystem:WaitForChild("Leaderboard")
local Space = Leaderboard:WaitForChild("Space")
local Place = Leaderboard:WaitForChild("Place")
local LeaderboardGui = Leaderboard:WaitForChild("SurfaceGui")
local Frame = LeaderboardGui:WaitForChild("Frame")

-- Checkpoint Variables
local CheckpointsFolder = TimerSystem:WaitForChild("CheckpointsFolder")
local Checkpoint1 = CheckpointsFolder:WaitForChild("Checkpoint1")
local Checkpoint2 = CheckpointsFolder:WaitForChild("Checkpoint2")
local Checkpoint3 = CheckpointsFolder:WaitForChild("Checkpoint3")
local Checkpoint4 = CheckpointsFolder:WaitForChild("Checkpoint4")

-- Event Variables
local Bools = TimerSystem:WaitForChild("Bools")
local InProgress = Bools:WaitForChild("InProgress")
local Event100m = Bools:WaitForChild("Event100m")
local Event200m = Bools:WaitForChild("Event200m")
local Event400m = Bools:WaitForChild("Event400m")
local Event800m = Bools:WaitForChild("Event800m")
local Event1600m = Bools:WaitForChild("Event1600m")

-- Functions
FinishWall.Touched:connect(function(hit)
    if InProgress.Value == true then
        if Event100m.Value == true or Event200m.Value == true then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local debounce = FinishedFolder:FindFirstChild(player.Name)
            if player.Character and (not debounce) then
                local check = Checkpoint1.Container:FindFirstChild(player.Name)
                if check then
                    local finishtag = Instance.new("StringValue")
                    finishtag.Name = player.Name
                    finishtag.Value = player.Name
                    finishtag.Parent = FinishedFolder
                    local Placing = Instance.new("TextLabel")
                    Placing.Text = (Place.Value .. ") " .. player.Name .. " - " .. Clock.Text)
                    Placing.Size = UDim2.new(1, 0, 0.125, 0)
                    Placing.Position = UDim2.new(0, 0, Space.Value, 0)
                    Placing.TextColor3 = Color3.new(85/255, 255/255, 255/255)
                    Placing.TextStrokeColor3 = Color3.new(255/255, 255/255, 255/255)
                    Placing.Font = "SourceSans"
                    Placing.FontSize = "Size42"
                    Placing.BackgroundTransparency = 1
                    Placing.TextStrokeTransparency = 0
                    Space.Value = Space.Value + 0.125
                    Place.Value = Place.Value + 1
                    Placing.Parent = Frame
                end
            end
        elseif Event400m.Value == true then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local debounce = FinishedFolder:FindFirstChild(player.Name)
            if player.Character and (not debounce) then
                local check = Checkpoint1.Container:FindFirstChild(player.Name)
                if check then
                    local finishtag = Instance.new("StringValue")
                    finishtag.Name = player.Name
                    finishtag.Value = player.Name
                    finishtag.Parent = FinishedFolder
                    local Placing = Instance.new("TextLabel")
                    Placing.Text = (Place.Value .. ") " .. player.Name .. " - " .. Clock.Text)
                    Placing.Size = UDim2.new(1, 0, 0.125, 0)
                    Placing.Position = UDim2.new(0, 0, Space.Value, 0)
                    Placing.TextColor3 = Color3.new(85/255, 255/255, 255/255)
                    Placing.TextStrokeColor3 = Color3.new(255/255, 255/255, 255/255)
                    Placing.Font = "SourceSans"
                    Placing.FontSize = "Size42"
                    Placing.BackgroundTransparency = 1
                    Placing.TextStrokeTransparency = 0
                    Space.Value = Space.Value + 0.125
                    Place.Value = Place.Value + 1
                    Placing.Parent = Frame
                end
            end
        elseif Event800m.Value == true then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local debounce = FinishedFolder:FindFirstChild(player.Name)
            if player.Character and (not debounce) then
                local check = Checkpoint2.Container:FindFirstChild(player.Name)
                if check then
                    local finishtag = Instance.new("StringValue")
                    finishtag.Name = player.Name
                    finishtag.Value = player.Name
                    finishtag.Parent = FinishedFolder
                    local Placing = Instance.new("TextLabel")
                    Placing.Text = (Place.Value .. ") " .. player.Name .. " - " .. Clock.Text)
                    Placing.Size = UDim2.new(1, 0, 0.125, 0)
                    Placing.Position = UDim2.new(0, 0, Space.Value, 0)
                    Placing.TextColor3 = Color3.new(85/255, 255/255, 255/255)
                    Placing.TextStrokeColor3 = Color3.new(255/255, 255/255, 255/255)
                    Placing.Font = "SourceSans"
                    Placing.FontSize = "Size42"
                    Placing.BackgroundTransparency = 1
                    Placing.TextStrokeTransparency = 0
                    Space.Value = Space.Value + 0.125
                    Place.Value = Place.Value + 1
                    Placing.Parent = Frame
                end
            end
        elseif Event1600m.Value == true then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local debounce = FinishedFolder:FindFirstChild(player.Name)
            if player.Character and (not debounce) then
                local check = Checkpoint4.Container:FindFirstChild(player.Name)
                if check then
                    local finishtag = Instance.new("StringValue")
                    finishtag.Name = player.Name
                    finishtag.Value = player.Name
                    finishtag.Parent = FinishedFolder
                    local Placing = Instance.new("TextLabel")
                    Placing.Text = (Place.Value .. ") " .. player.Name .. " - " .. Clock.Text)
                    Placing.Size = UDim2.new(1, 0, 0.125, 0)
                    Placing.Position = UDim2.new(0, 0, Space.Value, 0)
                    Placing.TextColor3 = Color3.new(85/255, 255/255, 255/255)
                    Placing.TextStrokeColor3 = Color3.new(255/255, 255/255, 255/255)
                    Placing.Font = "SourceSans"
                    Placing.FontSize = "Size42"
                    Placing.BackgroundTransparency = 1
                    Placing.TextStrokeTransparency = 0
                    Space.Value = Space.Value + 0.125
                    Place.Value = Place.Value + 1
                    Placing.Parent = Frame
                end
            end
        end
    end
end)

Checkpoint1.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event100m.Value == true or Event200m.Value == true or Event400m.Value == true or Event800m.Value == true or Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint1.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local checkpointtag = Instance.new("StringValue")
            checkpointtag.Name = player.Name
            checkpointtag.Value = player.Name
            checkpointtag.Parent = Checkpoint1.Container
        end
    end
end
end)

Checkpoint2.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event800m.Value == true or Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint2.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local check = Checkpoint1.Container:FindFirstChild(player.Name)
            if check then
                local checkpointtag = Instance.new("StringValue")
                checkpointtag.Name = player.Name
                checkpointtag.Value = player.Name
                checkpointtag.Parent = Checkpoint2.Container
            end
        end
    end
end
end)

Checkpoint3.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint3.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local check = Checkpoint2.Container:FindFirstChild(player.Name)
            if check then
                local checkpointtag = Instance.new("StringValue")
                checkpointtag.Name = player.Name
                checkpointtag.Value = player.Name
                checkpointtag.Parent = Checkpoint3.Container
            end
        end
    end
end
end)

Checkpoint4.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint4.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local check = Checkpoint3.Container:FindFirstChild(player.Name)
            if check then
                local checkpointtag = Instance.new("StringValue")
                checkpointtag.Name = player.Name
                checkpointtag.Value = player.Name
                checkpointtag.Parent = Checkpoint4.Container
            end
        end
    end
end
end)
0
Tip: It would be better if you posted the part of the code where the error happens, instead of all of your code Rare_tendo 3000 — 4y
0
Well its mostly Line 139-150 Damileminer300 -3 — 4y

4 answers

Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
4 years ago
Edited 4 years ago

You need to check if the thing that touches the Checkpoints is a player, you can do this by detecting if the thing that touched the checkpoint has a humanoid as players contain humanoids.

Checkpoint1.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event100m.Value == true or Event200m.Value == true or Event400m.Value == true or Event800m.Value == true or Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint1.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local checkpointtag = Instance.new("StringValue")
            checkpointtag.Name = player.Name
            checkpointtag.Value = player.Name
            checkpointtag.Parent = Checkpoint1.Container
        end
    end
end
end)

Checkpoint2.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event800m.Value == true or Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint2.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local check = Checkpoint1.Container:FindFirstChild(player.Name)
            if check then
                local checkpointtag = Instance.new("StringValue")
                checkpointtag.Name = player.Name
                checkpointtag.Value = player.Name
                checkpointtag.Parent = Checkpoint2.Container
            end
        end
    end
end
end)

Checkpoint3.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint3.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local check = Checkpoint2.Container:FindFirstChild(player.Name)
            if check then
                local checkpointtag = Instance.new("StringValue")
                checkpointtag.Name = player.Name
                checkpointtag.Value = player.Name
                checkpointtag.Parent = Checkpoint3.Container
            end
        end
    end
end
end)

Checkpoint4.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    if Event1600m.Value == true then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local debounce = Checkpoint4.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local check = Checkpoint3.Container:FindFirstChild(player.Name)
            if check then
                local checkpointtag = Instance.new("StringValue")
                checkpointtag.Name = player.Name
                checkpointtag.Value = player.Name
                checkpointtag.Parent = Checkpoint4.Container
            end
        end
    end
end
end)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

An if statement checking if the player is nil should get rid of the error.

EDIT: Maybe at the top get the Players service using game:GetService("Players") and turn it into a variable to reference it instead of using game.Players.

Checkpoint1.Touched:connect(function(hit)
    if Event100m.Value == true or Event200m.Value == true or Event400m.Value == true or Event800m.Value == true or Event1600m.Value == true then
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if player == nil then return end -- Added an if statement
        local debounce = Checkpoint1.Container:FindFirstChild(player.Name)
        if player.Character and (not debounce) then
            local checkpointtag = Instance.new("StringValue")
            checkpointtag.Name = player.Name
            checkpointtag.Value = player.Name
            checkpointtag.Parent = Checkpoint1.Container
        end
    end
end)
Log in to vote
0
Answered by 4 years ago

So it fixed most of my problems but the following line is having issues.

local debounce = Checkpoint1.Container:FindFirstChild(player.Name)
Log in to vote
0
Answered by 4 years ago

you problem is this,

something else is hitting your finishWall, CheckPoint1, CheckPoint2, but that is not a player, I guess that is your base plate.

0
I tried deleting my baseplate and all it says is that this line is broken --- local debounce = Checkpoint1.Container:FindFirstChild(player.Name) Damileminer300 -3 — 4y

Answer this question