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

How to use pcall with InvokeClient?

Hey all! Having a bit of trouble. I've checked the wiki, and I'm not sure what I'm doing wrong in this script. I've taken out parts, since they wouldn't be necessary for this question, and just put a print. Any tips? Thanks!

Server Side:

local success, result = pcall(function() return game.Workspace.BailScript.MoneyEvent:InvokeClient(script.Parent.Parent.Parent, script.Parent.Amount.Value, script.Parent.Parent.Parent.Name) end)
if success then
	if result == "Transaction Completed." and on == false then
		print('Success')
	else
		print('Error')
	end
end)

Client Side:

FE = script:WaitForChild("MoneyEvent")

function FE.OnClientInvoke(Amount, Player)
	local Response = game.ReplicatedStorage:WaitForChild("Events").rFunction:InvokeServer("Subtract",Amount,Player)
	return Response
end

1 answer

✓ Accepted answer

You are using OnClientInvoke wrong, it is a callback not a event. Callbacks I like to think of as properties,. they can only be set to one value. So if you had two scripts trying to use that RemoteFunction then only one (the last one to get set) would do anything. Callbacks also take Lua functions as their value. Example:

RemoteEvent.OnClientInvoke = function(Amount, Player)
	--code
end)

Now for your script we just change line 3 of the Client Side. The Server side is perfectly fine. But I might suggest changing line 6 of it to error(result).

FE = script:WaitForChild("MoneyEvent")

FE.OnClientInvoke = function(Amount, Player)
    local Response = game.ReplicatedStorage:WaitForChild("Events").rFunction:InvokeServer("Subtract",Amount,Player)
    return Response
end

Hope this helped!

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