InvokeServer() returns nil

Hey!

I’m at the moment trying to create a client-based animal spawn system. I’m invoking the spawn function from the client-side and returning a remote function from the server-side for later use. My problem is then … the remote event I create in a module that is required from the server script and then returned to the client gets nil. I can’t really understand what causes the remote function to end up nil. Here is the script if any of u clever heads can tell me what I’m doing wrong it would really help me on my way.

Client:

if currentClientAnimal < 2 then
		currentClientAnimal = currentClientAnimal + 1	
		local onAnimalRemoved, animalRemove = game.ReplicatedStorage.CollectionEventsLibrary.Server.Create_Animal_Invoke:InvokeServer(character:GetPrimaryPartCFrame().p + Vector3.new(math.random(-100,100),math.random(-100,100),math.random(-100,100)), animals[math.random(1,#animals)].Name)
		print(onAnimalRemoved, animalRemove)	
		local onAnimalRemovedConnection
		onAnimalRemovedConnection = onAnimalRemoved.OnClientEvent:Connect(function()
			currentClientAnimal = currentClientAnimal - 1
			onAnimalRemovedConnection:Disconnect()
		end)
	end
end

Sever:

function createAnimal(player, pos, key)
	local btree = animals.BTree:FindFirstChild(key)
	local animal = animals.Animal:FindFirstChild(key)
	ContentProvider:PreloadAsync(btree.Animations:GetChildren())
	local onAnimalRemoved, animalRemove = SpawnService.createAnimal(player, btree, animal, pos)
	print(onAnimalRemoved, animalRemove)	
	return onAnimalRemoved, animalRemove
end

game.ReplicatedStorage.CollectionEventsLibrary.Server.Create_Animal_Invoke.OnServerInvoke = createAnimal

module 1:

function SpawnService.createAnimal(player, btree, animal, pos)
	local self = setmetatable({}, SpawnService)
	local animalreftable = {}

	for ref, refdata in pairs(Settings["AnimalSettings"][btree.Name]["ref"]) do
		animalreftable[ref] = refdata
	end

	animalreftable.model = animal:Clone()
	animalreftable.spawnSpot = pos
	local onAnimalRemoved, animalRemove = AnimalService.new(player, animalreftable)
	return onAnimalRemoved, animalRemove
end

module 2:

… a lot of ai-coding which I can’t share, unfortunately. But no loops causing the module not to return!

here is the last part of the module instead:

     local maid = Maid.new()
	maid.onAnimalRemoved = Instance.new("RemoteEvent")
maid.animalRemove = Instance.new("RemoteEvent")

...

maid:GiveTask(treeState.model.Humanoid.HealthChanged:Connect(function(health)
    		if (health == 0) then
    			treeState.Blackboard.IsDead = (health == 0)
    			RemoveAnimal()
    		end
    	end))
    	
    	maid.animalRemove.OnServerEvent:Connect(function()
    		RemoveAnimal()
    	end)
    	
    	return maid.onAnimalRemoved, maid.animalRemove
    end

All help is appreciated :slight_smile:

When you create the remote event on the server, do you parent it to something that is accessible to the client? If it doesn’t exist in the instance hierarchy on the client, it’s nil on the client. If you only want a spesific client to see it, you can parent it to the playergui of that client. Otherwise, you can parent it to ReplicatedStorage.

My bad totally forgot that - thx