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

bad argument #1 to 'len' (string expected, got function)? How do i fix That?

In order to show you the script i had to make it a past bin link because its having problems on here. Link:http://pastebin.com/3Lg4HkJv The Error is below were i marked it as :-- RbxApi Stuff Some Variables

-- Services
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Workspace = game:GetService("Workspace")
local Debris = game:GetService("Debris")
local ContentProvider = game:GetService("ContentProvider")

-- Functions
function httpGet(url)
	return game:HttpGet(url,true)
end

-- RbxApi Stuff

local apiUrl = "http://anaminus.github.io/rbx/json/api/latest.json"
local maxChunkSize = 100 * 1000
local ApiJson
if script:FindFirstChild("RawApiJson") then
	ApiJson = script.RawApiJson
else
	ApiJson = ""
end
local function getLocalApiJson()
local rawApiJson = im puting this part of the = in pastbin because too long http://pastebin.com/PxUCfrft

Main Error

local function getCurrentApiJson()
	local jsonStr = nil
	local success, err = pcall(function()
		jsonStr = HttpService:GetAsync(apiUrl)
		print("Fetched json successfully")
	end)
	if success then
		print("Returning json")
		print(jsonStr:sub(1,500))
		return jsonStr
	else
		print("Error fetching json: " .. tostring(err))
		print("Falling back to local copy")
		return getLocalApiJson()
	end
end
local function splitStringIntoChunks(jsonStr)
	-- Splits up a string into a table with a given size
	local t = {}
	for i = 1, math.ceil(string.len(jsonStr)/maxChunkSize) do
		local str = jsonStr:sub((i-1)*maxChunkSize+1, i*maxChunkSize)
		table.insert(t, str)
	end
	return t
end

3 comments

1

Ehh... What line?

0

Error lines: 61, 58 called from 69, 69 utilizes variable line 68, 42 called by line 68. If anyone wants to backtrack that, go ahead. I have no idea where the error starts but hopefully this is a general idea. What all is printing?

0

local function splitStringIntoChunks(jsonStr) -- Splits up a string into a table with a given size local t = {} for i = 1, math.ceil(string.len(jsonStr)/maxChunkSize) do local str = jsonStr:sub((i-1)maxChunkSize+1, imaxChunkSize) table.insert(t, str) end return t end

1 answer

✓ Accepted answer

getCurrentApiJson calls getLocalApiJson in a tail-call, which returns a function. The result is caught in a variable.

splitStringIntoChunks is then called with that variable as first argument, assigning it to its jsonStr parameter. It then calls string.len with jsonStr as first argument, causing an error as string.len expects a string, not a function.

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