Returning pcall information to client?

Pardon my knowledge, but I’m quite bad with pcalls. If I wrap something in pcall, how would I return that the code errored to the client. I’m not sure if I’m doing it correctly?

local id
local success, errormessage = pcall(function()
id = game.Players:GetUserIdFromNameAsync(Input) -- Input is a text
end)

The thing is if input is wrong, or nil then it gives me an error. Is that how pcall’s are meant to work or what? I’m trying to send a remote event to the client to say your input is invalid, but the error doesn’t allow me.

Not understanding much, but, would this work:

local id
local HadSuccess, Returned = pcall(function()
	id = game.Players:GetUserIdFromNameAsync(Input)
	
	if id then
		return true -- Valid
	else
		return false -- Invalid
	end
end)

if HadSuccess and Returned then
	print(Returned) 
end

?

1 Like

Well I used your code but the pcall isn’t working or something isn’t. If the player’s id doesn’t exist it would error. In the pcall, where it says invalid, I sent a remote event back to the client but it doesn’t even work.

Use a remote function

local Players = game:GetService("Players")

remote_function.OnServerInvoke = function(player, input)
    return pcall(Players.GetUserIdFromNameAsync, Players, input)
end

So you are returning if it was successful and the result.

1 Like

Assuming Input is a string and not a number, and assuming you haven’t done this yet, have you tried using tonumber(Input)?

1 Like

@xxIamInevitable, Well yeah input is a string. I tried tonumber and I get the same error. I’m not even sure if that’s intended pcall behaviour?

@sjr04 So I would call this on the server, and it’s returns the result to the client?

2 Likes

Yeah, everything’s working as expected now. Thanks Incapaz and others!

I have one last question if anyone’s willing to answer. The remote function returns if it’s successful but how would I hook up something to change on the client. For example, if it’s successful the gui textbox background would change to green and vice verca?

1 Like

Players have a folder called PlayerGui, which is all where the GUI instances put in StarterGui go to.

1 Like