I’m trying to pass InvokeServer() as a variable for argument purposes.
local lol, text = game:GetService("ReplicatedStorage").Junk.Junk:InvokeServer(script.Parent.Parent.TextBox.Text, script.Parent.Parent.ATM.Value)
if lol == true then
print(text)
else
print(text)
end
I am doing this to return multiple arguments at once.
local KS = tonumber(Number)
if not KS then
return false, "NOT A NUMBER"
end
Are you getting an error in the output? I would try breaking down game:GetService("ReplicatedStorage").Junk.Junk into separate variables and printing them to make sure it’s all loading properly.
Most common issue is the client is trying to access it before it actually has the availability to do so. You should be cleaning your client-code, especially code that may potentially run while the client is still loading in.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local junkFunction: RemoteFunction = ReplicatedStorage:WaitForChild("Junk"):WaitForChild("Junk")
local text: string = script.Parent.Parent:WaitForChild("TextBox").Text
local atmValue: number = script.Parent.Parent:WaitForChild("ATM").Value
local success, response = junkFunction:InvokeServer(text, atmValue)
-- success handler
It is also likely that you are improperly handling a LuaTuple type, what I mean by this is you may not actually be returning a tuple on your OnServerInvoke callback. You are more or less treating it as a pcall(), which, in practice, you should most definitely do for error handling for RemoteFunction’s; however, you’re not actually wrapping it in a pcall.
-- server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local junkFunction: RemoteFunction = ReplicatedStorage.Junk.Junk
junkFunction.OnServerInvoke = function(player, text: string, atmValue: number)
-- handle request here
return success, response
end
This by no means is not what your code should end up looking like, as this is extremely flawed style-wise, also doesn’t account for if the server invocation server-code errors and then it’s return gets dropped, and if WaitForChild never succeeds. If you’re going to use RemoteFunctions, I would really recommend promisifying your data and acknowledging that there is a chance it will error. This is just an example of what you should look to change. Having :WaitForChild() also able to run on an infinite loop will at some point error saying it could never find it and that there was a potential infinite loop, and if it can’t find it: issue solved!
I forgot to pass a third argument of which the OnServerInvoke function was needing. resulting in a nil error I didn’t notice, but your snippet managed to clean things up!
Oh! Yes the server-side callback function’s first parameter is the player who called it. [RemoteFunction.OnServerInvoke]
Without it, you will end up shifting your variables as follows: player: Player, text: string, atmValue: number
to text: Player, atmValue: string, ?: number
I would imagine if you compiled a book of testimonials from people who’ve made this mistake you would run out of paper before you could publish it. The best mitigation strategy I’ve found is to just print the function parameters first thing whenever anything strange happens.
You’re so right. It’s such a common mistake so you can’t blame anyone
The best idea honestly is to make a wrapper module for Remotes that you force a middleware function into, so you don’t have to constantly call print.