HTTPService attempt to index nil with 'document'

Hello guys, hope you have a good day, I’m trying to make an crosschat for guilded and roblox but there’s a problem, Everytime I try get variables it gives me this error “attempt to index nil with ‘document’” and the nil thing is not nil at all and when I try to print it it says “nil” and I don’t know how to fix this, can anyone help?

Code:

local httpService = game:GetService("HttpService")
local GuildedUrl = "https://www.guilded.gg/api/channels/05b502ea-5872-43b4-a3f8-cb33f11835fe/messages?limit=1"
local LastGuildedMsg = nil
local LastRobloxMsg= nil
local Webhook = "hidden"
local Cookies = "hidden"

game.Players.PlayerAdded:Connect(function(p)
	p.Chatted:Connect(function(m)

		local json2 = "{'content': '"..m.."', 'username': '[Roblox] '"..p.Name.."}"
		local msgJson = httpService:JSONDecode(json2)
		local request = httpService:PostAsync(Webhook, "", Enum.HttpContentType.ApplicationJson, true, msgJson)
		
		
	end)
end)

while wait() do
	local json = httpService:JSONDecode(Cookies)
	local request = httpService:GetAsync(GuildedUrl, false, json)
	local req = httpService:JSONDecode(request)
	print(req.messages.content)
	local message = req["messages"]["content"]["document"]["nodes"]["nodes"]["leaves"]["text"]
	local username = req["messages"]["createdBy"]
	
	if message ~= LastGuildedMsg then
		LastGuildedMsg = message
		game.ReplicatedStorage.SendMsgInChat:FireAllClients(message, username)
	end
	
	wait(2)
end

The error is in line 24 by the way.

What does this print? If the request fails, your code will error, because you’re referencing things that don’t exist if the request is nil.

This line of code just prints “nil” but when I print req.messages alone I see content in it.

What exactly does req.messages output?

Roblox doesn’t work with ‘content’. If there’s no key ‘content’ in req.messages, req.messages.content will evaluate as nil, meaning your code crashes (because instead of checking if it exists, you just use the value even though it is nil)

{
[1] = ▼ {
[“channelId”] = “05b502ea-5872-43b4-a3f8-cb33f11835fe”,
[“content”] = ▼ {
[“document”] = ▼ {
[“data”] = {},
[“nodes”] = ▼ {
[1] = ▼ {
[“data”] = {},
[“nodes”] = ▼ {
[1] = ▼ {
[“leaves”] = ▼ {
[1] = ▼ {
[“marks”] = {},
[“object”] = “leaf”,
[“text”] = “The Bros”
}
},
[“object”] = “text”
}
},
[“object”] = “block”,
[“type”] = “paragraph”
}
},
[“object”] = “document”
},
[“object”] = “value”
},
[“createdAt”] = “2024-09-19T15:27:28.411Z”,
[“createdBy”] = “dNrxaROd”,
[“id”] = “4c8d9186-d8f6-495a-b8b4-9c40a74b66db”,
[“isPinned”] = false,
[“isPrivate”] = false,
[“isSilent”] = false,
[“reactions”] = {},
[“repliesToIds”] = {},
[“type”] = “default”
}
}

i want to get the “The Bros” variable but i cant, also this is what it prints btw

See, req.messages doesn’t have a key ‘content’, because there is a key ‘1’ before it.
req.messages[1].content.document.nodes[1].leaves[1].text
But you should add a condition; check if req.messages is nil first. If it is, do not continue.

1 Like

Alright i’ma check if this works

This one works! thank you so much for your help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.