Problem with Client Script

So I’m having an issue with how I’m gonna get the Values out of an API response table! It keeps throwing Warnings and I don’t know what I’m doing wrong!
Here is my Script:

-- Services

local UserService = game:GetService("UserService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Variables

local GUI = script.Parent
local Backgrond = GUI:WaitForChild("Background")
local MainFrame = Backgrond:WaitForChild("Main")
local UsernameInput = MainFrame:WaitForChild("Username")
local Username = MainFrame:WaitForChild("UsernameLabel")
local DisplayName = MainFrame:WaitForChild("Displayname")
local UserIcon = MainFrame:WaitForChild("UserIcon")
local placeID = MainFrame:WaitForChild("Place-ID")
local JobID = MainFrame:WaitForChild("Job-ID")
local LastOnline = MainFrame:WaitForChild("LastOnline")
local Remote = ReplicatedStorage:WaitForChild("Player")

-- Functions


local function Input(text : string)
	
	local data = Players:GetUserIdFromNameAsync(text)
	
	Remote:FireServer(data)
	
end


--local function calculateLastOnline(lastOnlineString)
--	local year, month, day, hour, min, sec = lastOnlineString:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+.%d+)Z")

--	if year and month and day and hour and min then
--		return string.format("%02d:%02d:%02d:%02d:%02d", tonumber(min), tonumber(hour), tonumber(day), tonumber(month), tonumber(year))
--	else
--		return "Invalid last online format"
--	end
--end

local function Setup(response)
	if response.userPresences and #response.userPresences > 0 then
		local userPresence = response.userPresences[1]
		local userId = userPresence.userId

		local dn = UserService:GetUserInfosByUserIdsAsync({userId})
		local Un = Players:GetNameFromUserIdAsync(userId)
		local icon = Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)

		DisplayName.Text = dn[1].DisplayName
		Username.Text = "@" .. Un
		UserIcon.Image = icon
		placeID.Text = "Place ID: " .. userPresence.placeId
		JobID.Text = "Job ID: " .. userPresence.gameId
		--LastOnline.Text = "Last Online: " .. calculateLastOnline(userPresence.lastOnline)
	else
		warn("No user presences found in the response.")
	end
end



UsernameInput.FocusLost:Connect(function(enter)
	
	if enter then
		
		Input(UsernameInput.Text)
		
	end
	
end)

Remote.OnClientEvent:Connect(Setup)

Warnings I get: No user presences found in the response.

This is the response table: {"userPresences":[{"userPresenceType":3,"lastLocation":"Studio - ","placeId":null,"rootPlaceId":null,"gameId":null,"universeId":null,"userId":1329875485,"lastOnline":"2023-11-07T20:40:40.73Z"}]}

If someone could fix the calculateLastOnline thingy, that would be pretty cool!

Have you tried printing response.userPresences to see if it’s nil?

Hmm it’s nil, but idk how to fix it :frowning:

Did you add the response as a parameter to Setup via the Client event handler? Remote.OnClientEvent:Connect(Setup(response))

Yes I did it via the Server. Here’s the Server Script if it helps in any way:

-- Services

local HTTP = game:GetService("HttpService")
local ReplicatedS = game:GetService("ReplicatedStorage")

-- Variables

local URL = "https://presence.roproxy.com/v1/presence/users"
local Remote = ReplicatedS:FindFirstChild("Player")

-- Function

local function getPresenceData(plr : Player, userID : number)
	
	local presenceData = {}
	
	local data = HTTP:JSONEncode({
		["userIds"] = {userID}
	})
	
	local succ, response = pcall(function()
		
		return HTTP:PostAsync(URL, data)
		
	end)
	
	if succ then
		
		print(response)
		HTTP:JSONDecode(response)
		Remote:FireClient(plr, response)
		
	else
		
		warn("There was an error while accessing presence.roblox.com : " .. response)
		
	end
	
end

Remote.OnServerEvent:Connect(getPresenceData)

I assumed, but you never plugged response into your Setup function where you handle the client event which is why response is nil

The Setup function has the parameter response

it would be Remote.OnClientEvent:Connect(Setup(response))

You might be right actually, I think im being dumb

image
The OnClientEvent already handles that!

1 Like

Yeah I see now, can you try printing just response on the client?

Same table like on the Server:

{"userPresences":[{"userPresenceType":3,"lastLocation":"Studio - ","placeId":null,"rootPlaceId":null,"gameId":null,"universeId":null,"userId":1329875485,"lastOnline":"2023-11-07T21:14:44.693Z"}]}

I don’t understand.

can you do print(typeof(response))

It returns a string?! Shouldn’t this return table.

Yes, it should be returning a table. That’s where your problem is coming from.

And how am I gonna put it into a table?!

I believe this is because you meant to have HTTP:JSONDecode(response) as your parameter for response? Correct me if i’m wrong.

Yes, that’s right!
image

I’m confused how you’re using it as I don’t really expertise in the api usage, but does HTTP:JSONDecode(response) permanently convert the response variable to a table? I would assume you’d use it at Remote:FireClient(plr, HTTP:JSONDecode(response))

Yeah, like the person above said, you didn’t set the response variable to the JSONDecoded function. Also, send the code next time in text instead of an image so it’s easier to help fix it.