Error: attempt to index nil with 'FindFirstChild' How to fix this?
Asked by
4 years ago Edited 4 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:
002 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
003 | local ServerStorage = game:GetService( "ServerStorage" ) |
004 | local Players = game:GetService( "Players" ) |
007 | local StagesFolder = ServerStorage:WaitForChild( "Stages" ) |
008 | local Stages = StagesFolder:GetChildren() |
011 | local Tower = game.Workspace:WaitForChild( "Tower" ) |
012 | local Storage = game.Workspace:WaitForChild( "Storage" ) |
013 | local Lobby = Storage:WaitForChild( "Lobby" ) |
014 | local End = Storage:WaitForChild( "End" ) |
015 | local SpawnsModel = Lobby:WaitForChild( "Spawns" ) |
016 | local Spawns = SpawnsModel:GetChildren() |
017 | local Songs = game.Workspace:WaitForChild( "Songs" ) |
018 | local WinTp = workspace.WinTp |
023 | local TimerSpeed = ReplicatedStorage:WaitForChild( "TimerSpeed" ) |
024 | local Minutes = ReplicatedStorage:WaitForChild( "Minutes" ) |
025 | local Seconds = ReplicatedStorage:WaitForChild( "Seconds" ) |
026 | local TimerTag = ReplicatedStorage:WaitForChild( "TimerTag" ) |
027 | local MultiplierVisible = ReplicatedStorage:WaitForChild( "MultiplierVisible" ) |
028 | local Multiplies = ReplicatedStorage:WaitForChild( "Multiplies" ) |
029 | local TowerHeight = ReplicatedStorage:WaitForChild( "TowerHeight" ) |
030 | local MutatorsFolder = ReplicatedStorage:WaitForChild( "Mutators" ) |
042 | Floor 1 = Stages [ math.random( 1 , #Stages) ] :Clone() |
043 | Floor 1. Parent = Tower |
044 | Floor 1. PrimaryPart = Floor 1 :FindFirstChild( "Center" ) |
045 | Floor 1 :SetPrimaryPartCFrame(Floor 1. Center.CFrame) |
046 | local Floor 1 Height = Floor 1. Height.Value |
048 | Floor 2 = Stages [ math.random( 1 , #Stages) ] :Clone() |
049 | Floor 2. Parent = Tower |
050 | Floor 2. PrimaryPart = Floor 2 :FindFirstChild( "Center" ) |
051 | Floor 2 :SetPrimaryPartCFrame(Floor 1. Center.CFrame + Vector 3. new( 0 , Floor 1 Height, 0 )) |
052 | Floor 2 :SetPrimaryPartCFrame(Floor 2. Center.CFrame * CFrame.Angles( 0 , math.rad( 180 ), 0 )) |
053 | local Floor 2 Height = Floor 2. Height.Value |
055 | Floor 3 = Stages [ math.random( 1 , #Stages) ] :Clone() |
056 | Floor 3. Parent = Tower |
057 | Floor 3. PrimaryPart = Floor 3 :FindFirstChild( "Center" ) |
058 | Floor 3 :SetPrimaryPartCFrame(Floor 2. Center.CFrame + Vector 3. new( 0 , Floor 2 Height, 0 )) |
059 | Floor 3 :SetPrimaryPartCFrame(Floor 3. Center.CFrame * CFrame.Angles( 0 , math.rad( 180 ), 0 )) |
060 | local Floor 3 Height = Floor 3. Height.Value |
062 | Floor 4 = Stages [ math.random( 1 , #Stages) ] :Clone() |
063 | Floor 4. Parent = Tower |
064 | Floor 4. PrimaryPart = Floor 4 :FindFirstChild( "Center" ) |
065 | Floor 4 :SetPrimaryPartCFrame(Floor 3. Center.CFrame + Vector 3. new( 0 , Floor 3 Height, 0 )) |
066 | Floor 4 :SetPrimaryPartCFrame(Floor 4. Center.CFrame * CFrame.Angles( 0 , math.rad( 180 ), 0 )) |
067 | local Floor 4 Height = Floor 4. Height.Value |
069 | Floor 5 = Stages [ math.random( 1 , #Stages) ] :Clone() |
070 | Floor 5. Parent = Tower |
071 | Floor 5. PrimaryPart = Floor 5 :FindFirstChild( "Center" ) |
072 | Floor 5 :SetPrimaryPartCFrame(Floor 4. Center.CFrame + Vector 3. new( 0 , Floor 4 Height, 0 )) |
073 | Floor 5 :SetPrimaryPartCFrame(Floor 5. Center.CFrame * CFrame.Angles( 0 , math.rad( 180 ), 0 )) |
074 | local Floor 5 Height = Floor 5. Height.Value |
076 | End.PrimaryPart = End:WaitForChild( "Center" ) |
077 | End:SetPrimaryPartCFrame(Floor 5. Center.CFrame + Vector 3. new( 0 , Floor 5 Height), 0 ) |
078 | End:SetPrimaryPartCFrame(End.Center.CFrame * CFrame.Angles( 0 , math.rad( 180 ), 0 )) |
081 | TowerHeight.Value = Floor 1 Height + Floor 2 Height + Floor 3 Height + Floor 4 Height + Floor 5 Height + 15 * 2 |
082 | ReplicatedStorage.HeightEvent:FireAllClients(TowerHeight.Value) |
087 | print ( "Stopped Song" ) |
089 | RandomSong = Songs:GetChildren() [ math.random( 1 , #Songs:GetChildren()) ] |
092 | for i, v in pairs (Players:GetChildren()) do |
093 | v.Character.Head.CFrame = Spawns [ math.random( 1 , #Spawns) ] .CFrame |
095 | print ( "Teleported Players To Lobby" ) |
096 | for i, v in pairs (MutatorsFolder:GetChildren()) do |
098 | print ( "Cleared Mutators" ) |
101 | print ( "Reset Minutes" ) |
103 | print ( "Reset Seconds" ) |
105 | print ( "Reset Timer Speed" ) |
106 | MultiplierVisible.Value = false |
107 | print ( "Mutiplier Visible Set To False" ) |
109 | print ( "Multiplies Value Set To 1" ) |
110 | Tower:ClearAllChildren() |
111 | print ( "Cleared Tower" ) |
116 | RandomSong = Songs:GetChildren() [ math.random( 1 , #Songs:GetChildren()) ] |
121 | MultiplierVisible.Value = false |
127 | if Minutes and Seconds and TimerSpeed and Tower and Stages then |
129 | if Seconds.Value < = 9 then |
130 | TimerTag.Value = tostring (Minutes.Value).. ":0" .. tostring (Seconds.Value) |
132 | TimerTag.Value = tostring (Minutes.Value).. ":" .. tostring (Seconds.Value) |
134 | if Seconds.Value < = 0 then |
135 | Minutes.Value = Minutes.Value - 1 |
138 | Seconds.Value = Seconds.Value - 1 |
140 | wait(TimerSpeed.Value) |
141 | until Minutes.Value < = 0 and Seconds.Value < = 0 |
142 | if Minutes.Value < = 0 and Seconds.Value < = 1 then |
143 | TimerTag.Value = "0:00" |
145 | for i, v in pairs (Players:GetChildren()) do |
146 | if v.Character:FindFirstChild( "Win" ) then |
147 | v.Character.Win:Destroy() |
149 | if v:FindFirstChild( "Backpack" ) then |
150 | v.Backpack:ClearAllChildren() |
154 | print ( "Stopped Song" ) |
156 | RandomSong = Songs:GetChildren() [ math.random( 1 , #Songs:GetChildren()) ] |
159 | for i, v in pairs (Players:GetChildren()) do |
160 | v.Character.Head.CFrame = Spawns [ math.random( 1 , #Spawns) ] .CFrame |
162 | print ( "Teleported Players To Lobby" ) |
163 | for i, v in pairs (MutatorsFolder:GetChildren()) do |
165 | print ( "Cleared Mutators" ) |
168 | print ( "Reset Minutes" ) |
170 | print ( "Reset Seconds" ) |
172 | print ( "Reset Timer Speed" ) |
173 | MultiplierVisible.Value = false |
174 | print ( "Mutiplier Visible Set To False" ) |
176 | print ( "Multiplies Value Set To 1" ) |
177 | Tower:ClearAllChildren() |
178 | print ( "Cleared Tower" ) |