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

Attempt to index a nil value fix?

Can anyone help me fix a script? he error in the output 20:28:22.853 - ServerScriptService.MainScript:250: attempt to index a nil value

the script:

local maps = game.ServerStorage:WaitForChild("Minigames")
local tools = game.ServerStorage:WaitForChild("Tools")
local InMatchColor = "Really red"
local InLobbyColor = "White"
local map = {}
math.randomseed(tick())
local Sync = game.ReplicatedStorage:WaitForChild("Sync")
local Sync2 = game.ReplicatedStorage:WaitForChild("Sync2")
script:WaitForChild("Config")
local PointsService = game:GetService("PointsService")

function BroadcastInstructions(text)
	local gui = Instance.new("ScreenGui",game.StarterGui)
	local frame = Instance.new("Frame",gui)
	frame.Size = UDim2.new(0,700,0,400)
	frame.BackgroundColor = BrickColor.new(0,0,255)
	frame.BorderColor3 = Color3.new(0,0,.5)
	frame.Position = UDim2.new(-1,-100,.5,-180)
	frame.BorderSizePixel = 10
	frame.BackgroundTransparency = 0.5
	local title = Instance.new("TextLabel",frame)
	title.Size = UDim2.new(1,0,0,50)
	title.BackgroundTransparency = 1
	title.FontSize = "Size48"
	title.Font = "SourceSansBold"
	title.TextColor3 = Color3.new(255,255,255)
	title.TextStrokeTransparency = 0
	title.Text = "Instructions"
	local textlabel = Instance.new("TextLabel",frame)
	textlabel.Size = UDim2.new(0,700,0,400)
	textlabel.BackgroundTransparency = 1
	textlabel.FontSize = "Size24"
	textlabel.Font = "SourceSansBold"
	textlabel.TextColor3 = Color3.new(255,255,255)
	textlabel.TextStrokeTransparency = 0
	textlabel.Text = text
	textlabel.TextWrapped = true
	for i,v in next, game.Players:GetPlayers() do	
		local instructionsgui = gui:Clone()
		instructionsgui.Parent = v.PlayerGui
		local endd = script.remve:Clone()
		endd.Parent = instructionsgui
		endd.Disabled = false
	end
end

function WhoSurvived(text)
	local gui = Instance.new("ScreenGui",game.StarterGui)
	local frame = Instance.new("Frame",gui)
	frame.Size = UDim2.new(0,700,0,400)
	frame.BackgroundColor = BrickColor.new(0,255,0)
	frame.BorderColor3 = Color3.new(0,.5,0)
	frame.Position = UDim2.new(-1,-100,.5,-180)
	frame.BorderSizePixel = 10
	frame.BackgroundTransparency = 0.5
	local title = Instance.new("TextLabel",frame)
	title.Size = UDim2.new(1,0,0,50)
	title.BackgroundTransparency = 1
	title.FontSize = "Size48"
	title.Font = "SourceSansBold"
	title.TextColor3 = Color3.new(255,255,255)
	title.TextStrokeTransparency = 0
	title.Text = "Survivors"
	local textlabel = Instance.new("TextLabel",frame)
	textlabel.Size = UDim2.new(0,700,0,400)
	textlabel.BackgroundTransparency = 1
	textlabel.FontSize = "Size24"
	textlabel.Font = "SourceSansBold"
	textlabel.TextColor3 = Color3.new(255,255,255)
	textlabel.TextStrokeTransparency = 0
	textlabel.Text = text
	textlabel.TextWrapped = true
	for i,v in next, game.Players:GetPlayers() do	
		local instructionsgui = gui:Clone()
		instructionsgui.Parent = v.PlayerGui
		local endd = script.remve:Clone()
		endd.Parent = instructionsgui
		endd.Disabled = false
	end
end

function NoSurvivors()
	local gui = Instance.new("ScreenGui",game.StarterGui)
	local frame = Instance.new("Frame",gui)
	frame.Size = UDim2.new(0,700,0,400)
	frame.BackgroundColor = BrickColor.new(255,0,0)
	frame.BorderColor3 = Color3.new(.5,0,0)
	frame.Position = UDim2.new(-1,-100,.5,-180)
	frame.BorderSizePixel = 10
	frame.BackgroundTransparency = 0.5
	local title = Instance.new("TextLabel",frame)
	title.Size = UDim2.new(1,0,0,50)
	title.BackgroundTransparency = 1
	title.FontSize = "Size48"
	title.Font = "SourceSansBold"
	title.TextColor3 = Color3.new(255,255,255)
	title.TextStrokeTransparency = 0
	title.Text = "UH OH!!"
	local textlabel = Instance.new("TextLabel",frame)
	textlabel.Size = UDim2.new(0,700,0,400)
	textlabel.BackgroundTransparency = 1
	textlabel.FontSize = "Size48"
	textlabel.Font = "SourceSansBold"
	textlabel.TextColor3 = Color3.new(255,255,255)
	textlabel.TextStrokeTransparency = 0
	textlabel.Text = "NO SURVIVORS!!"
	textlabel.TextWrapped = true
	for i,v in next, game.Players:GetPlayers() do	
		local instructionsgui = gui:Clone()
		instructionsgui.Parent = v.PlayerGui
		local endd = script.remve:Clone()
		endd.Parent = instructionsgui
		endd.Disabled = false
	end
end

local TeleportPlayers = function()
	local spawns = newmap:FindFirstChild("Spawns")
	if spawns then
		for i,v in next, game.Players:GetPlayers() do
			if v.TeamColor == BrickColor.new(InMatchColor) then
				v:LoadCharacter()
				v.Character:MoveTo(spawns:GetChildren()[math.random(#spawns:GetChildren())].Position)
				local ff = Instance.new("ForceField",v.Character)
				game.Debris:AddItem(ff,.01)
			end
		end
	end
end

local PlayersInMatch = function()
	local amount = 0
	local lastPlr = nil
	for i,v in next, game.Players:GetPlayers() do
		if v.TeamColor == BrickColor.new(InMatchColor) then
			amount = amount + 1
			lastPlr = v
		end
	end
	if amount == 1 then
		return amount,lastPlr
	end
	return amount
end

function newMap()
    local maps = game.ServerStorage.Minigames
    local mapfolder = game.Workspace.MapFolder

    local allmaps = maps:GetChildren()
	newmap = allmaps[math.random(1,#allmaps)]:clone()

    newmap.Parent = mapfolder
	Sync:FireAllClients("Minigame chosen:")
	Sync2:FireAllClients(newmap.Name)
	wait(3)
	
    return newmap
end

local Lobby = function()
	for i = script.Config.LobbyTime.Value,0,-1 do
		workspace.SoundEffects.Beep:Play()
		Sync:FireAllClients("Intermission")
		Sync2:FireAllClients(i)
		wait(1)
	end
end

local End = function()
	for i,v in next, game.Players:GetPlayers() do
		if v.TeamColor == BrickColor.new(InMatchColor) then
			v.CameraMode = "Classic"
			if v.Character then
				v.Character:BreakJoints()
				v.TeamColor = BrickColor.new(InLobbyColor)
			end
		end
	end
end

workspace.MapFolder:ClearAllChildren()
while true do
	print("success")
	wait()
	repeat
		wait()
		Sync:FireAllClients("Can't start yet")
		Sync2:FireAllClients("Waiting for 2 players...")
	until game.Players.NumPlayers >= 2
	Lobby()
	local trackslobby=workspace.MusicLobby:GetChildren()
	local chooselobby=math.random(1,#trackslobby)
	local trackl=trackslobby[chooselobby]
	trackl:Play()
	for i,v in next, game.Players:GetPlayers() do
		v.TeamColor = BrickColor.new(InMatchColor)
	end
	wait()
	Sync:FireAllClients("Choosing minigame")
	Sync2:FireAllClients("Minigame not yet chosen")
	wait(3)
	local map = newMap()
	BroadcastInstructions(newmap.instructions.Value)
	wait(5)
	Sync:FireAllClients("It's time!")
	Sync2:FireAllClients("Teleporting players...")
	wait(3)
	for i,v in next, game.Players:GetPlayers() do
		if v.TeamColor == BrickColor.new(InMatchColor) then
			local effect = script.TransitionEffect:Clone()
			effect.Parent = v.PlayerGui
			effect.LocalScript.Disabled = false
		end
	end
	wait(2)
	trackl:Stop()
	TeleportPlayers()
	for i = 10, 1, -1 do
		workspace.SoundEffects.Beep:Play()
		Sync:FireAllClients("Get ready!")
		Sync2:FireAllClients(i)
		wait(1)
	end
	for i,v in next, game.Players:GetPlayers() do
		if v.TeamColor == BrickColor.new(InMatchColor) then
		end
	end
	newmap.Minigame.Disabled = false
	Sync:FireAllClients("Begin!")
	Sync2:FireAllClients("Survive!")
	workspace.SoundEffects.DING:Play()
	local tracksgame=workspace.MusicGame:GetChildren()
	local choosegame=math.random(1,#tracksgame)
	local trackg=tracksgame[choosegame]
	trackg:Play()
	wait(1)
	for i = newmap.durationtime.Value,0,-1 do 
		Sync:FireAllClients(newmap.Name)
		Sync2:FireAllClients(i)
		wait(1)
		local PlrsLeft,LastPlr = PlayersInMatch()
		if LastPlr then
			trackg:Stop()
			workspace.SoundEffects.DING:Play()
			workspace.SoundEffects.VICTORY:Play()
			Sync:FireAllClients("Game over!")
			Sync2:FireAllClients("Results are in!")
			WhoSurvived(LastPlr.Name)
			LastPlr:FindFirstChild("leaderstats"):FindFirstChild("Points").Value = LastPlr.leaderstats.Points.Value + script.Config.PointsAwarded.Value
			PointsService:AwardPoints(LastPlr.userId, 100)
			break
		elseif PlrsLeft < 1 then
			trackg:Stop()
			workspace.SoundEffects.DING:Play()
			workspace.SoundEffects.LOSE:Play()
			Sync:FireAllClients("Game over!")
			Sync2:FireAllClients("Results are in!")
			NoSurvivors()
			break
		end
		if i == 0 then
			local PlrText = ""
			for i,v in next, game.Players:GetPlayers() do
				if v.TeamColor == BrickColor.new(InMatchColor) then
					PlrText = PlrText..v.Name..","
					v:FindFirstChild("leaderstats"):FindFirstChild("Points").Value = v.leaderstats.Points.Value + script.Config.PointsAwarded.Value
					PointsService:AwardPoints(v.userId, 100)
				end
			end
			trackg:Stop()
			workspace.SoundEffects.DING:Play()
			workspace.SoundEffects.VICTORY:Play()
			Sync:FireAllClients("Game over!")
			Sync2:FireAllClients("Results are in!")
			WhoSurvived(PlrText:sub(1,#PlrText - 1))
			break
		end
	end
	wait(2)
	newmap:remove()
	End()
	wait(5)
	workspace.SoundEffects.LOSE:Stop()
	workspace.SoundEffects.DING:Stop()
	workspace.SoundEffects.VICTORY:Stop()
	workspace.SoundEffects.Beep:Stop()
end

2 comments

0

Simply remove the :FindFirstChild()'s also workspace is deprecated, use game.Workspace

0

You should have just included the lines that mattered. No one wants to read 200 something unrelated lines of code.

1 answer

✓ Accepted answer

The problem is at line 250 roblox couldn't find the object you were looking for and returned nil straight way. The best solution is to perform the WaitForChild method rather than the FindFirstChild method.

1 comment

0

Thank you!

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