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

Invalid argument for 'pairs' (table expected, got string), but argument is a table?

I'm trying to issue a post request via RemoteFunction, but the in pairs() loop is not working.

I'm getting an error trying to tell me im using the incorrect datatype for the 'pairs', but the argument is a table.

Server script:

local http = game:GetService('HttpService')
local database = 'http://boomlings.com/database/'

game.ReplicatedStorage:WaitForChild('IssuePostRequest').OnServerInvoke = function(php, param)
	local data = ''
		
	for i, v in pairs(param) do
		data = data .. ('&%s=%s'):format(
			http:UrlEncode(i),
			http:UrlEncode(v)
		)
	end
	
	data = data:sub(2)
	print(data)
		
	local returned
		
	local success, eMes = pcall(function()
		returned = http:PostAsync(database..php, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
	end)
		
	if success then
		return {'Success', returned}
	else
		return {'Error', eMes}
	end
end

Local script:

local CAS = game:GetService('ContextActionService')
local secretC = 'Wmfd2893bg7'

CAS:BindAction('Response', function()
	local response = game.ReplicatedStorage:WaitForChild('IssuePostRequest'):InvokeServer('getGJUserInfo20.php', {
		secret = secretC,
		targetAccountID = 7362432
	})
	
	if response[1] == 'Success' then
		print(response[2][3])
	else
		warn(response[2])
	end
	
end, false, 'e')

You are free to correct my Http code, because I am extremely new to it.

1 answer

✓ Accepted answer

The reason why is that Roblox thinks php is the player, because RemoteEvents and RemoteFunctions automatically fire Player as the first argument, and your setting php to as the parameter for the player, to fix this instead of

game.ReplicatedStorage:WaitForChild('IssuePostRequest').OnServerInvoke = function(php, param)

It should be

game.ReplicatedStorage:WaitForChild('IssuePostRequest').OnServerInvoke = function(player, php, param)

1 comment

0

Can't believe i forgot that. Thank you!

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