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
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)
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.