Hi, this is an irritating error. I finished my gamescript, plays all rounds and stuff. However, after a couple of rounds, it errors with this:
"ServerScriptService.Game:31: attempt to index global 'typeName' (a nil value)"
And the script stop working.
I have no idea how to fix this, its driving me crazy!!
status = game.ServerScriptService.Status local type = {"Meteor Shower","Raining Noobs","Moon","Eerie Darkseed","Tsunami","Master Hand","Acid Rain","Deadly Egg Rain","Rising Lava","Epic Duck","Raining Knives","Shoop Da Whoop"} sfx = {} map = {} leaderboard = nil local w = game.Workspace:getChildren() for i=1,#w do if w[i].Name == "leaderboard" and w[i]:findFirstChild("running") ~= nil and w[i]:findFirstChild("points") ~= nil then leaderboard = w[i] end end for i,game in pairs (game.ServerStorage:FindFirstChild("Maps"):GetChildren()) do table.insert(map,#map+1,game.Name) end for i,game in pairs (game.ServerStorage:FindFirstChild("SoundTrack"):GetChildren()) do table.insert(sfx,#sfx+1,game.Name) end while true do print(#map) mapName = map[math.random(1, #map)] mapName = game.ServerStorage.Maps:FindFirstChild(mapName) mapClone = mapName:Clone() mapClone.Parent = game.Workspace mapClone:MakeJoints() print (#type) typeName = type[math.random(1, #type)] typeName = game.ServerStorage.enemies:FindFirstChild(typeName) typeClone = typeName:Clone() game.Workspace.Lobby:Play() for i = 60, 1, -1 do status.Value = "Round 1/5 will start in: "..i wait(1) end game.Workspace.Lobby:stop() status.Value = "Disaster: "..typeClone.Name wait(3) sfxName = sfx[math.random(1, #sfx)] sfxName = game.ServerStorage.SoundTrack:FindFirstChild(sfxName) sfxClone = sfxName:Clone() sfxClone.Parent = game.Workspace sfxClone:play() workspace.points.Value = 20 game.Workspace.running.Value = true typeClone.Parent = game.Workspace typeClone:MakeJoints() for i = 30, 1, -1 do status.Value = "Time until round ends: "..i wait(1) end typeClone:remove() sfxClone:Stop() sfxClone:destroy() game.Workspace.running.Value = false status.Value = "Results!" wait(5) print (#type) typeName = type[math.random(1, #type)] typeName = game.ServerStorage.enemies:FindFirstChild(typeName) typeClone = typeName:Clone() game.Workspace.Lobby:Play() for i = 15, 1, -1 do status.Value = "Round 2/5 will start in: "..i wait(1) end game.Workspace.Lobby:stop() status.Value = "Disaster: "..typeClone.Name wait(3) sfxName = sfx[math.random(1, #sfx)] sfxName = game.ServerStorage.SoundTrack:FindFirstChild(sfxName) sfxClone = sfxName:Clone() sfxClone.Parent = game.Workspace sfxClone:play() workspace.points.Value = 20 game.Workspace.running.Value = true typeClone.Parent = game.Workspace typeClone:MakeJoints() for i = 30, 1, -1 do status.Value = "Time until round ends: "..i wait(1) end typeClone:remove() sfxClone:Stop() sfxClone:destroy() game.Workspace.running.Value = false status.Value = "Results!" wait(5) print (#type) typeName = type[math.random(1, #type)] typeName = game.ServerStorage.enemies:FindFirstChild(typeName) typeClone = typeName:Clone() game.Workspace.Lobby:Play() for i = 15, 1, -1 do status.Value = "Round 3/5 will start in: "..i wait(1) end game.Workspace.Lobby:stop() status.Value = "Disaster: "..typeClone.Name wait(3) sfxName = sfx[math.random(1, #sfx)] sfxName = game.ServerStorage.SoundTrack:FindFirstChild(sfxName) sfxClone = sfxName:Clone() sfxClone.Parent = game.Workspace sfxClone:play() workspace.points.Value = 20 game.Workspace.running.Value = true typeClone.Parent = game.Workspace typeClone:MakeJoints() for i = 30, 1, -1 do status.Value = "Time until round ends: "..i wait(1) end typeClone:remove() sfxClone:Stop() sfxClone:destroy() game.Workspace.running.Value = false status.Value = "Results!" wait(5) print (#type) typeName = type[math.random(1, #type)] typeName = game.ServerStorage.enemies:FindFirstChild(typeName) typeClone = typeName:Clone() game.Workspace.Lobby:Play() for i = 15, 1, -1 do status.Value = "Round 4/5 will start in: "..i wait(1) end game.Workspace.Lobby:stop() status.Value = "Disaster: "..typeClone.Name wait(3) sfxName = sfx[math.random(1, #sfx)] sfxName = game.ServerStorage.SoundTrack:FindFirstChild(sfxName) sfxClone = sfxName:Clone() sfxClone.Parent = game.Workspace sfxClone:play() workspace.points.Value = 20 game.Workspace.running.Value = true typeClone.Parent = game.Workspace typeClone:MakeJoints() for i = 30, 1, -1 do status.Value = "Time until round ends: "..i wait(1) end typeClone:remove() sfxClone:Stop() sfxClone:destroy() game.Workspace.running.Value = false status.Value = "Results!" wait(5) print (#type) typeName = type[math.random(1, #type)] typeName = game.ServerStorage.enemies:FindFirstChild(typeName) typeClone = typeName:Clone() game.Workspace.Lobby:Play() for i = 15, 1, -1 do status.Value = "Round 5/5 will start in: "..i wait(1) end game.Workspace.Lobby:stop() status.Value = "Disaster: "..typeClone.Name wait(3) sfxName = sfx[math.random(1, #sfx)] sfxName = game.ServerStorage.SoundTrack:FindFirstChild(sfxName) sfxClone = sfxName:Clone() sfxClone.Parent = game.Workspace sfxClone:play() workspace.points.Value = 20 game.Workspace.running.Value = true typeClone.Parent = game.Workspace typeClone:MakeJoints() for i = 30, 1, -1 do status.Value = "Time until round ends: "..i wait(1) end typeClone:remove() sfxClone:Stop() sfxClone:destroy() game.Workspace.running.Value = false status.Value = "Results!" wait(5) mapClone:Destroy() end
There is a ton of repetitive code here, so here is your code reduced using a for
loop:
while true do print(#map) mapName = map[math.random(1, #map)] mapName = game.ServerStorage.Maps:FindFirstChild(mapName) mapClone = mapName:Clone() mapClone.Parent = game.Workspace mapClone:MakeJoints() for ROUND = 1, 5 do print (#type) typeName = type[math.random(1, #type)] typeName = game.ServerStorage.enemies:FindFirstChild(typeName) typeClone = typeName:Clone() game.Workspace.Lobby:Play() for i = (ROUND == 1 and 60 or 15), 1, -1 do status.Value = "Round " .. ROUND .. "/5 will start in: "..i wait(1) end game.Workspace.Lobby:stop() status.Value = "Disaster: "..typeClone.Name wait(3) sfxName = sfx[math.random(1, #sfx)] sfxName = game.ServerStorage.SoundTrack:FindFirstChild(sfxName) sfxClone = sfxName:Clone() sfxClone.Parent = game.Workspace sfxClone:play() workspace.points.Value = 20 game.Workspace.running.Value = true typeClone.Parent = game.Workspace typeClone:MakeJoints() for i = 30, 1, -1 do status.Value = "Time until round ends: "..i wait(1) end typeClone:remove() sfxClone:Stop() sfxClone:destroy() game.Workspace.running.Value = false status.Value = "Results!" wait(5) end mapClone:Destroy() end
To address your actual issue, the only problem I can see here is that the chosen typeName
is not actually in ServerStorage.enemies.
Can you take a screenshot of the Explorer pane, with all the children of ServerStorage.enemies visible?
You're missing Parenthesis. Like in line 30 should be
typeName = game.ServerStorage.enemies:FindFirstChild("typeName")
I pretty sure thats your problem.
If this was helpful please Upvote **and **Accept answer!
I did Dewn's solution, it came up with 'attempt to index global 'dsastrName' (a nil value)' after a few rounds
type is a Lua keyword, change the variable name. If that doesn't work tell me.