InvokeServer() as variable does not invoke server

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
1 Like

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.

1 Like

You can just create a table that stores the arguments then unpack it on InvokeServer()

local Args = {1, true}
RemoteEvent:InvokeServer(unpack(Args))

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!

It’s not erroring at all.

The event is invoked by a mouseclick connection.

script.Parent.MouseButton1Down:Connect(function()
	local lol, text = game:GetService("ReplicatedStorage").Junk.Junk:InvokeServer(script.Parent.Parent.TextBox.Text, script.Parent.Parent.ATM.Value)
	if lol == true then
		txt.Text = text
		txt.TextTransparency = 0
		task.delay(1,function()
			parency:Play()
		end)
	else
		txt.TextColor3 = Color3.fromRGB(255, 60, 63)
		txt.Text = text
		txt.TextTransparency = 0
		task.delay(1,function()
			parency:Play()
			game:GetService("TweenService"):Create(txt, TweenInfo.new(1.5), {TextColor3 = Color3.fromRGB(255, 255, 255)}):Play()
		end)
	end
end)

script.Parent.Parent.TextBox.Text is a number while script.Parent.Parent.ATM.Value is an object value. Actually found the issue.

What was the issue you were having? You marked my post as the solution but you said it wasn’t erroring

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!

1 Like

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

1 Like

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.

1 Like

Or you can just use

OnServerInvoke = function(...)
 print(...)
end

You’re so right. It’s such a common mistake so you can’t blame anyone :sob:
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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.