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

Attempted to index nil with TextLabel fix?

I am trying to make a police radio system and i am getting an nil error i am not able to fix I hope someonehere knows what the issue is.

Exact error: Players.loganmatt3.PlayerGui.RadioGui.ServerRadioScript:5: attempt to index nil with 'LineSix'

Script where error comes from:

local RadioRemoteEvents = game.ReplicatedStorage.RadioRemoteEvents
local RadioEvent = RadioRemoteEvents.RadioEvent

function DisplayMessage(Frame,Message)
	Frame.LineSix.Text = Frame.LineFive.Text 
	Frame.LineFive.Text = Frame.LineFour.Text 
	Frame.LineFour.Text = Frame.LineThree.Text 
	Frame.LineThree.Text = Frame.LineTwo.Text 
	Frame.LineTwo.Text = Frame.LineOne.Text 
	Frame.LineOne.Text = Message
end

function ConfigMessage(Player,Message,Channel)
	local RadioGui = script.Parent
	local Frame = RadioGui.RadioFrame:FindFirstChild(Channel)
	local ConfiguredMessage = Player.Name..": "..Message
	DisplayMessage(Frame,ConfiguredMessage)
end

RadioEvent.OnServerEvent:Connect(function(Player,Message,Channel)
	ConfigMessage(Player,Message,Channel)
end)

3 answers

There is no item with the name of the Channel variable inside RadioGui.RadioFrame.

The message isn't exactly intuitive I understand but in simpler terms it mean's you can't get the child LineSix of nil, something that doesn't exist.

Ensure that the 'Channel' argument getting sent to the client is the name of a direct child of RadioFrame.

Try replacing the .s with:FindFirstChild() the code should look like this.

local RadioRemoteEvents = game.ReplicatedStorage.RadioRemoteEvents
local RadioEvent = RadioRemoteEvents.RadioEvent

function DisplayMessage(Frame,Message)
    Frame:FindFirstChild("LineSix").Text = Frame:FindFirstChild("LineFive").Text
    Frame:FindFirstChild("LineFive").Text = Frame:FindFirstChild("LineFour").Text
    Frame:FindFirstChild("LineFour").Text = Frame:FindFirstChild("LineThree").Text
    Frame:FindFirstChild("LineThree").Text = Frame:FindFirstChild("LineTwo").Text
    Frame:FindFirstChild("LineTwo").Text = Frame:FindFirstChild("LineOne").Text
    Frame:FindFirstChild("LineOne").Text = Message
end

function ConfigMessage(Player,Message,Channel)
    local RadioGui = script.Parent
    local Frame = RadioGui.RadioFrame:FindFirstChild(Channel)
    local ConfiguredMessage = Player.Name..": "..Message
    DisplayMessage(Frame,ConfiguredMessage)
end

RadioEvent.OnServerEvent:Connect(function(Player,Message,Channel)
    ConfigMessage(Player,Message,Channel)
end)

I really hope this helps! If this did help marking it as the answer would be good. If you have any other problems or this didn't work comment on this and we can test more things, lets get that radio workin'!

The structure of your script checks out, but you're not checking to see if things exist before calling/writing to them.

I've done a little example (ill paste the script(s) here at the end of this answer for reference)

The code I've written checks to see if our gui elements exist before printing. If they do exist, then continue as normal if they don't exist. For example, if we misspelled LineFour as LneFour or even linefour then the code will tell you that this doesn't exist in your display(s) and lets you know what display its missing.

Modified Code as follows:

ServerCode:

local RadioRemoteEvents = game.ReplicatedStorage.RadioRemoteEvents
local RadioEvent = RadioRemoteEvents.RadioEvent

function DisplayMessage(Frame,Message)
	
	local LineOne = Frame:FindFirstChild("LineOne")
	local LineTwo = Frame:FindFirstChild("LineTwo")
	local LineThree = Frame:FindFirstChild("LineThree")
	local LineFour = Frame:FindFirstChild("LineFour")
	local LineFive = Frame:FindFirstChild("LineFive")
	local LineSix = Frame:FindFirstChild("LineSix")
	
	-- this embedded ambigus function simply returns the above values in a table to see if all exist
	-- But this shouldn't be this complex but Roblox lua returns a table element that doesn't exist as void and not nil! **Pulls out hair!**
	local CheckTble = (function()
		local Tab = {LineOne,LineTwo,LineThree,LineFour,LineFive,LineSix}
		local RetTab = {} 
		for k,v in pairs(Tab) do
			if(v ~= nil) then
				RetTab[#RetTab+1] = v
			end
		end
		return RetTab
	end)()
	-- Now we can simply check to see if the above table has less then our lines (in this case 6) if it is less than 6 then error out with a helpfull message in output!
	if(#CheckTble ~= 6) then
		return 
			error("Error: Malformed Display in "..tostring(Frame)
				.." \n Object location = "..tostring(Frame:GetFullName())
				.." !")
	end
	-- Everything checked out fine, Lets continue as normal!...
	LineSix.Text = Frame.LineFive.Text 
	LineFive.Text = Frame.LineFour.Text 
	LineFour.Text = Frame.LineThree.Text 
	LineThree.Text = Frame.LineTwo.Text 
	LineTwo.Text = Frame.LineOne.Text 
	LineOne.Text = Message
end

function ConfigMessage(Player,Message,Channel)
	local RadioGui = script.Parent
	local Frame = RadioGui.RadioFrame:FindFirstChild(Channel)
	if(Channel ~= nil) then
		local ConfiguredMessage = Player.Name..": "..Message
		DisplayMessage(Frame,ConfiguredMessage)
	else
		error("Channel["..tostring(Channel).."] Does not exist in 'RadioGui.RadioFrame'!") 
	end
end

RadioEvent.OnServerEvent:Connect(function(Player,Message,Channel)
	ConfigMessage(Player,Message,Channel)
end)

PlayerGui (Local Script) Code:

local TextBox = script.Parent.TextBox
local SubmitBtn = script.Parent["Submit Button"]
local EventCaller_Storage = game:GetService("ReplicatedStorage")
local EventCaller_FDR = EventCaller_Storage:WaitForChild("RadioRemoteEvents",10)
local EventCaller = EventCaller_FDR:WaitForChild("RadioEvent",10)

SubmitBtn.MouseButton1Click:Connect(function() 
	local Message = TextBox.Text
	EventCaller:FireServer(Message,"Channel One")
end)

Hope this helps! :)

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