InvokeServer not working

I am trying to do InvokeServer on a remote function, however it does not work, it tries to invoke server, but the server script never responds to it

Local Script

game:GetService("ReplicatedStorage"):WaitForChild("GuiFunctions", 30)
local Frame = script.Parent
Frame.Draggable = true
local FunctionsModule = require(script:WaitForChild("ModuleScript", 30))
for _, v in pairs(Frame:GetChildren()) do
	if not v:IsA("TextBox") and not v:IsA("TextLabel") and v ~= script then
		v.MouseButton1Click:Connect(function()
			FunctionsModule["On"..v.Name.."Clicked"](game.Players.LocalPlayer, Frame)
		end)
	end
end

Module Script

local RS = game:GetService("ReplicatedStorage")
RS:WaitForChild("GuiFunctions", 30)
local Busy = false
return{
	OnCreateClicked = function(Player: Player, Frame: Frame)
		print("Create")
		if not Busy then
			print("Not Busy")
			Busy = true
			print("Firing")
			RS.GuiFunctions.Create:InvokeServer(Frame.Username.Text, Frame.UserID.Text)
			print("Fired")
			RS.GuiFunctions.Create.OnClientInvoke = function()
				Busy = false
			end
		end
	end,
}

Server Script

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Rigs = 1

RS:WaitForChild("GuiFunctions", 30)
RS.GuiFunctions.Create.OnServerInvoke = function(Player: Player, Username: string, UserID: number)
	print("CreateS")
	local NewRig
	local s, _ = pcall(function()
		if game.Players:GetUserIdFromNameAsync(Username) then
			NewRig = Players:CreateHumanoidModelFromDescription(Players:CreateHumanoidModelFromUserId(Players:GetUserIdFromNameAsync(Username)).Humanoid.HumanoidDescription, Enum.HumanoidRigType.R15, Enum.AssetTypeVerification.Always)
		elseif game.Players:GetPlayerByUserId(UserID) then
			NewRig = Players:CreateHumanoidModelFromDescription(Players:CreateHumanoidModelFromUserId(UserID).Humanoid.HumanoidDescription, Enum.HumanoidRigType.R15, Enum.AssetTypeVerification.Always)
		end
	end)
	if s and NewRig then
		Rigs += 1
		NewRig.Name = "Rig"..tostring(Rigs)
	else
		warn(_)      --Added to see if any errors happen
	end
	NewRig.Parent = workspace
	task.wait()
	RS.GuiFunctions.Create:InvokeClient(Player)
end
2 Likes

I believe the issue is in the very last line of the code. In order to respond to InvokeServer, you just need to do return, not :InvokeClient() This is just not the right way to use it.

Your server script will look like this:

NewRig.Parent = workspace
task.wait()
return VALUE_TO_SEND_BACK

Also, InvokeClient() should be avoided due to some risks.

1 Like

Gotcha, also the main problem is that the server script is supposed to print something out when the function is invoked, however nothing is printed

1 Like

Does the client print “Fired” when :InvokeServer() is called? There might be an issue with the local scritping having an error or yielding infinitely earlier in the script.

Before it invokes the function it prints “Firing”, after it’s invoked the function it prints “Fired”, “Firing” is printed, but “Fired” is not printed, and the server also does not print

Do you see anything in the output: warnings/errors etc?

Only one unrelated warning
Screenshot 2023-11-21 213426

1 Like

Try removing the WaitForChild for GuiFunctions on the server

1 Like

You may be correct. I tried running a simplified version of the script and I am able to get OnServerInvoke to run. It seems like something is preventing it from being set before InvokeServer() is called.

1 Like

I did, same result unfortunately

I’m gonna try to have it invoke server on the actual local script instead of the module script to see if that’ll fix it

That didn’t work either, I’m gonna try rewriting the whole thing instead

The issue is that OnClientInvoke is placed before InvokeServer. This means that, at the moment when InvokeClient is ran, OnClientInvoke has not yet been set, meaning that nothing happens and the script is stuck in an infinite wait. I applied the following fix to get this result:
image

MODULE

if not Busy then
	print("Not Busy")
	Busy = true
	print("Firing")
	Busy = RS.GuiFunctions.Create:InvokeServer(Frame.Username.Text, Frame.UserID.Text) -- We can send back the value we want to set
	print("Fired")
end

SERVER

NewRig.Parent = workspace
task.wait()
return false -- We return this value and then set Busy to it
1 Like

As for the reason why InvokeServer isn’t working at all, I can’t fully tell why that is happening, but it’s likely that something is preventing OnServerInvoke from being set before InvokeSever() is ran. On my simplified version of the script, it seemed to be working. There is something hindering it higher in the code.

Yes, this was causing it:

This wasn’t included in the code because I didn’t think it would causes any issues

if game.ReplicatedStorage.RemoteEvent.OnServerEvent:Wait() == true then
	script:Destroy()
	task.wait(99999999999)
end

This waits for an event to be fired, and if it’s true (indicating that it’s going to reserve a server first), then it destroys itself, however a separate script would destroy this remote event before the script could receive it:

game.Players.PlayerAdded:Connect(function(Player: Player)
	local _, Reserve = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent", 5).OnServerEvent:Wait()
	game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent", 5):Destroy()

Thanks for all of your, and 0Enum’s help!

1 Like

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