Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My game script plays a few rounds, suddenly errors, and stops?

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

3 comments

0

What Line is the error in?

0

Continent, the error recorded is in line 31, but it can happen in any line thats has nameType, and I don't know why. (the error i recorded is ServerScriptService.Game:31: attempt to index global 'typeName' (a nil value)

0

Thats it it you're missing Parenthesis should be "TypeName"!

4 answers

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?

3 comments

0

It's erroring on 'Shoop Da Whoop'. In the game, you've spelt it without an 'h', but in the script, you spelt it with and 'h'.

0

Oh!! I'm such a dumbo sometimes.

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!

2 comments

0

Why is this a Downvote?

0

He has parenthesis and you don't put quotes around a variable name... (I did not down-vote you but I can see why you were)

I did Dewn's solution, it came up with 'attempt to index global 'dsastrName' (a nil value)' after a few rounds

2 comments

0

Did you spell that incorrectly? Because you never use that variable...

0

Please don't submit an answer to add a comment to your question. Either edit the question or post an actual comment.

type is a Lua keyword, change the variable name. If that doesn't work tell me.

2 comments

0

It Depends, Unless you add another Word or letter to it, like true=true BAD/truey=true GOOD, So that wouldn't bother.

0

type is actually not a Lua keyword, it's just a built-in function. You can overwrite it no problems, although that makes it impossible to actually use the type function.

This discussion is preserved as it was. New answers, comments, and votes are closed.