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

Error: attempt to index nil with 'FindFirstChild' How to fix this?

Asked by 3 years ago
Edited 3 years ago

I have a game similar to ToH. The tower changing script sometimes does not work. The script works fine with no problems for some time, then randomly it stops working. I get an error message on line 146 saying: attempt to index nil with 'FindFirstChild' This is my script:

-- Services Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

-- ServerStorage Variables
local StagesFolder = ServerStorage:WaitForChild("Stages")
local Stages = StagesFolder:GetChildren()

-- Workspace Variables
local Tower = game.Workspace:WaitForChild("Tower")
local Storage = game.Workspace:WaitForChild("Storage")
local Lobby = Storage:WaitForChild("Lobby")
local End = Storage:WaitForChild("End")
local SpawnsModel = Lobby:WaitForChild("Spawns")
local Spawns = SpawnsModel:GetChildren()
local Songs = game.Workspace:WaitForChild("Songs")
local WinTp = workspace.WinTp
local RandomSong
local RandomSpawn

-- ReplicatedStorage Variables
local TimerSpeed = ReplicatedStorage:WaitForChild("TimerSpeed")
local Minutes = ReplicatedStorage:WaitForChild("Minutes")
local Seconds = ReplicatedStorage:WaitForChild("Seconds")
local TimerTag = ReplicatedStorage:WaitForChild("TimerTag")
local MultiplierVisible = ReplicatedStorage:WaitForChild("MultiplierVisible")
local Multiplies = ReplicatedStorage:WaitForChild("Multiplies")
local TowerHeight = ReplicatedStorage:WaitForChild("TowerHeight")
local MutatorsFolder = ReplicatedStorage:WaitForChild("Mutators")

local Floor1
local Floor2
local Floor3
local Floor4
local Floor5

debounce = false

function NewTower()
    debounce = true
    Floor1 = Stages[math.random(1, #Stages)]:Clone()
    Floor1.Parent = Tower
    Floor1.PrimaryPart = Floor1:FindFirstChild("Center")
    Floor1:SetPrimaryPartCFrame(Floor1.Center.CFrame)
    local Floor1Height = Floor1.Height.Value
    wait()
    Floor2 = Stages[math.random(1, #Stages)]:Clone()
    Floor2.Parent = Tower
    Floor2.PrimaryPart = Floor2:FindFirstChild("Center")
    Floor2:SetPrimaryPartCFrame(Floor1.Center.CFrame + Vector3.new(0, Floor1Height, 0))
    Floor2:SetPrimaryPartCFrame(Floor2.Center.CFrame * CFrame.Angles(0, math.rad(180), 0))
    local Floor2Height = Floor2.Height.Value
    wait()
    Floor3 = Stages[math.random(1, #Stages)]:Clone()
    Floor3.Parent = Tower
    Floor3.PrimaryPart = Floor3:FindFirstChild("Center")
    Floor3:SetPrimaryPartCFrame(Floor2.Center.CFrame + Vector3.new(0, Floor2Height, 0))
    Floor3:SetPrimaryPartCFrame(Floor3.Center.CFrame * CFrame.Angles(0, math.rad(180), 0))
    local Floor3Height = Floor3.Height.Value
    wait()
    Floor4 = Stages[math.random(1, #Stages)]:Clone()
    Floor4.Parent = Tower
    Floor4.PrimaryPart = Floor4:FindFirstChild("Center")
    Floor4:SetPrimaryPartCFrame(Floor3.Center.CFrame + Vector3.new(0, Floor3Height, 0))
    Floor4:SetPrimaryPartCFrame(Floor4.Center.CFrame * CFrame.Angles(0, math.rad(180), 0))
    local Floor4Height = Floor4.Height.Value
    wait()
    Floor5 = Stages[math.random(1, #Stages)]:Clone()
    Floor5.Parent = Tower
    Floor5.PrimaryPart = Floor5:FindFirstChild("Center")
    Floor5:SetPrimaryPartCFrame(Floor4.Center.CFrame + Vector3.new(0, Floor4Height, 0))
    Floor5:SetPrimaryPartCFrame(Floor5.Center.CFrame * CFrame.Angles(0, math.rad(180), 0))
    local Floor5Height = Floor5.Height.Value
    wait()
    End.PrimaryPart = End:WaitForChild("Center")
    End:SetPrimaryPartCFrame(Floor5.Center.CFrame + Vector3.new(0, Floor5Height), 0)
    End:SetPrimaryPartCFrame(End.Center.CFrame * CFrame.Angles(0, math.rad(180), 0))
    debounce = false

    TowerHeight.Value = Floor1Height + Floor2Height + Floor3Height + Floor4Height + Floor5Height + 15 * 2
    ReplicatedStorage.HeightEvent:FireAllClients(TowerHeight.Value)
end

function NewRound()
    RandomSong:Stop()
    print("Stopped Song")
    wait()
    RandomSong = Songs:GetChildren()[math.random(1, #Songs:GetChildren())]
    RandomSong:Play()
    print("Played Song")
    for i, v in pairs(Players:GetChildren()) do
        v.Character.Head.CFrame = Spawns[math.random(1, #Spawns)].CFrame
    end
    print("Teleported Players To Lobby")
    for i, v in pairs(MutatorsFolder:GetChildren()) do
        v.Value = false
        print("Cleared Mutators")
    end
    Minutes.Value = 8
    print("Reset Minutes")
    Seconds.Value = 0
    print("Reset Seconds")
    TimerSpeed.Value = 1
    print("Reset Timer Speed")
    MultiplierVisible.Value = false
    print("Mutiplier Visible Set To False")
    Multiplies.Value = 1
    print("Multiplies Value Set To 1")
    Tower:ClearAllChildren()
    print("Cleared Tower")
    NewTower()
    print("New Tower")
end

RandomSong = Songs:GetChildren()[math.random(1, #Songs:GetChildren())]
RandomSong:Play()
Minutes.Value = 8
Seconds.Value = 0
TimerSpeed.Value = 1
MultiplierVisible.Value = false
Multiplies.Value = 1
debounce = true
NewTower()

while true do
    if Minutes and Seconds and TimerSpeed and Tower and Stages then
        repeat
            if Seconds.Value <= 9 then
                TimerTag.Value = tostring(Minutes.Value)..":0"..tostring(Seconds.Value)
            else
                TimerTag.Value = tostring(Minutes.Value)..":"..tostring(Seconds.Value)
            end
            if Seconds.Value <= 0 then
                Minutes.Value = Minutes.Value - 1
                Seconds.Value = 59
            else
                Seconds.Value = Seconds.Value - 1
            end
            wait(TimerSpeed.Value)
        until Minutes.Value <= 0 and Seconds.Value <= 0
        if Minutes.Value <= 0 and Seconds.Value <= 1 then
            TimerTag.Value = "0:00"
            wait(0.1)
            for i, v in pairs(Players:GetChildren()) do
                if v.Character:FindFirstChild("Win") then
                    v.Character.Win:Destroy()
                end
                if v:FindFirstChild("Backpack") then
                    v.Backpack:ClearAllChildren()
                end
            end
            RandomSong:Stop()
            print("Stopped Song")
            wait()
            RandomSong = Songs:GetChildren()[math.random(1, #Songs:GetChildren())]
            RandomSong:Play()
            print("Played Song")
            for i, v in pairs(Players:GetChildren()) do
                v.Character.Head.CFrame = Spawns[math.random(1, #Spawns)].CFrame
            end
            print("Teleported Players To Lobby")
            for i, v in pairs(MutatorsFolder:GetChildren()) do
                v.Value = false
                print("Cleared Mutators")
            end
            Minutes.Value = 8
            print("Reset Minutes")
            Seconds.Value = 0
            print("Reset Seconds")
            TimerSpeed.Value = 1
            print("Reset Timer Speed")
            MultiplierVisible.Value = false
            print("Mutiplier Visible Set To False")
            Multiplies.Value = 1
            print("Multiplies Value Set To 1")
            Tower:ClearAllChildren()
            print("Cleared Tower")
            NewTower()
            print("New Tower")
        end
    end
end

1 answer

Log in to vote
0
Answered by 3 years ago

Maybe try getting the Character with this:

v.Character or v.Characteradded:Wait():FindFirstChild("Win")

I'm not very sure if this actually works, but it's always worth a try!

0
I tried it and it gave this error: Win is not a valid member of Model "Workspace".SpongebobRules26 SpongebobRules26 0 — 3y
0
The error was on line 147: v.Character.Win:Destroy() SpongebobRules26 0 — 3y
Ad

Answer this question