Unable to cast to std::string error

I am currently creating a script that gets a user’s last online but I’m getting an error: Unable to cast to std::string


My script is:

local HttpProxy = require(game.Workspace.HttpProxyService:Clone()) -- Script that does all the stuff
local HttpService = game:GetService('HttpService')
function getUrl()
	local cooldown = 2.5
	local id = 1000
while true do
		local DecodedJSON = (HttpService:JSONDecode(HttpProxy:GetAsync('http://api.roblox.com/users/'.. tostring(id)..'/onlinestatus')))
		print(DecodedJSON)
		script.Parent.Text = DecodedJSON[4]
		id = id + 1
	wait(cooldown)
	end
end

getUrl()

What causes the error?

Technically, roblox games aren’t supposed to be able to access roblox urls with the Http service, which I’m guessing the proxy is for. It is an error in the proxy, so we’d need to see that as well. (I can tell its a proxy error because the table is not printing like it should with print(DecodedJSON) (If you want to see the contents of the table, print the non JSONDecoded version, as printing a table just prints table: 0x(Hex Numbers) and this isn’t very helpful for debugging.)

The Proxy module is:

local ScriptId = 'AKfycbzqPGyRZxrURwszw0P3NiTCTsI6sGmze6JkHpK_c1V7GQq_Fbw'

local Url = "https://script.google.com/macros/s/" .. ScriptId .. "/exec"
local HttpService = game:GetService('HttpService')
local Module = {}


function JSONDecode(JSON)
	local JSONTable = {} 
	pcall(function ()
		JSONTable = HttpService:JSONDecode(JSON)
	end) 
	return JSONTable
end


function Module:GetAsync(Link)
	local JSON = HttpService:GetAsync(Url .. "?q="..game.HttpService:UrlEncode(Link))
	local Result = JSONDecode(JSON)
	if Result.result == "success" then
		return Result.response
	else
		warn(tostring(Link).." failed to fetch because "..tostring(Result.error))
		return
	end
end

return Module

I found it on a Proxy tutorial on the devforum.

Your issue is that the module already encodes the returned data to a table, so no need for the HttpService:JSONDecode(HttpProxy:GetAsync('http://api.roblox.com/users/'.. tostring(id)..'/onlinestatus')), all you need is HttpProxy:GetAsync('http://api.roblox.com/users/'.. tostring(id)..'/onlinestatus')

Subdivide things like this next time to debug faster:

local ReturnedData = HttpProxy:GetAsync('http://api.roblox.com/users/'.. tostring(id)..'/onlinestatus')
print(ReturnedData)
local DecodedJSON = (HttpService:JSONDecode(ReturnedData))
print(DecodedJSON)

It prints a table, and trying to decode a table creates the error.

It instead prints the table but it gives the same error.

local HttpProxy = require(game.Workspace.HttpProxyService:Clone()) -- Script that does all the stuff
local HttpService = game:GetService('HttpService')

function getUrl()
	local id = 1000
	local cd = 2.5
	while true do
	local ReturnedData = HttpProxy:GetAsync('http://api.roblox.com/users/'.. tostring(id)..'/onlinestatus')
print(ReturnedData)
local DecodedJSON = (HttpService:JSONDecode(ReturnedData))
		print(DecodedJSON)
		id = id + 1
		wait(cd)
		end end

getUrl()

Is the problem something else?

local DecodedJSON = (HttpService:JSONDecode(ReturnedData)) You are still decoding the value, that is your error. the JSONDecode can only decode strings.

So I use tostring() and then print the value?
Edit: Didn’t work.

no.

Fixed code:

function getUrl()
	local id = 1000
	local cd = 2.5
	while true do
	    local ReturnedData = HttpProxy:GetAsync('http://api.roblox.com/users/'.. tostring(id)..'/onlinestatus')
		id = id + 1
		wait(cd)
		end end

Please read replies fully before replying or just copying code, reply #3 should have been enough if you had read it fully and not just copied the code that was there to show how to debug in small steps.

1 Like