Remote Function not working?

My remote function is supposed to spawn object from Replicated Storage to the workspace by pressing an UI. However its not working somehow and it doesn’t yield me an error?

--Local
local RS = game:GetService("ReplicatedStorage")
local Event = RS:WaitForChild("Events")
local SpawnFunction = Event:WaitForChild("SpawnUnit")
local Gui = script.Parent


for i,v in pairs(Gui.UnitList.Frame:GetChildren()) do
	if v:IsA("ImageButton") then
		v.Activated:Connect(function()
			print("Spawned " .. v.Name)
			SpawnFunction:InvokeServer(v.Name)
			print("Successfully spawned")
		end)
	end
end

--Module
local Unit = {}

local RS = game:GetService("ReplicatedStorage")
local Event = RS:WaitForChild("Events")
local SpawnFunction = Event:WaitForChild("SpawnUnit")

function Unit.SpawnUnit(unit)
	local tower = RS.Units:FindFirstChild(unit)
	local UnitClone = tower:Clone()
	UnitClone.Parent = workspace.Unit
end

SpawnFunction.OnServerInvoke = Unit.SpawnUnit

return Unit

Tried RemoteEvent instead and in server script. It now says “Attempt to index nil with clone”.

I’m not sure about this as I personally never use ServerInvoke.

You could try to put this in a ServerScriptService and try to call it off there.

This probably is a easy fix but i don’t see the issue here.

Oh I didn’t see that.

Hmmm the issue is that they return as Nil whenever i tries to call it via client.

This will sound unprofessional but try to call a remote event and from the server call the invoke the server.

This is my worst idea yet.

:thinking:

Or just read the documentation.

I’m pretty sure i have seen them from client.

  1. Unless you need the Unit object, don’t use a remote function use a remote event
  2. If you do need the Unit object your function doesn’t return it, so that is probably why it doesn’t work

So if i were to also make it move and attack from the module script, remote event would still be the best choice?

A remote function Returns something, this means that it will pause the script that called it (in this case the local script) until that something is returned.

So you only use it when you NEED to confirm that the event has been completed, and you are going to use the reference from the script that called it.

So to answer your question, yes it would work because the server would take over and handle the management of the unit, not the client.

I tried remote events now inside module script but it still doesnt work. And it also doesn’t give me any output too beside the “Successfully spawned” and “Spawned … Name” Print

Are you sure that RS.Units:FindFirstChild(unit) exists? It might be because there’s no child of RS.Units that has the name of the ‘unit’ parameter

yes. i fill the “unit” parameter by the button names which the code will find the object in the units folder
been 1 hour and still cant find a solution welp

Ok so the first parameter of the function will always be the player who fired, so you should
replace this:

function Unit.SpawnUnit(unit)

with this:

function Unit.SpawnUnit(player, unit)

and also I don’t think that OnServerEvent or OnServerInvoke will work or run on ModuleScripts

Now that i put it on Server script. I got an error
OnServerInvoke is a callback member of RemoteFunction; you can only set the callback value, get is not available.
What does that mean?

oh and fyi all im doing is basically making so if you place a TextButton (GUI) an object spawn from the server side

Try using Remote Event instead of Remote Function and see if it works now.

Remote function produces that error is probably because it has to return something, correct me if I’m wrong tho.

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