Why wont the .OnServerEvent() fire?

Everything works fine until the the .OnServerEvent() in the module.
On the client I retrieve the type of morph, then on the server I weld the morph to the player.

What could be the issue?

local RunService = game:GetService("RunService")
local Remote = script.Remotes:WaitForChild("EquipMorph")

local MorphEquipped = false


local Morph = {}

local function Weld(Character, MorphType)
end

function Morph.Init()	
	if RunService:IsServer() then
		
		local function Equip(Player, MorphType)
			local Character = Player.Character
			local Humanoid = Character:FindFirstChild("Humanoid")

			if not MorphEquipped then 

				for _, Hats in ipairs(Character:GetChildren()) do 
					if Hats:IsA("Accessory") then 
						Hats:Destroy()
					end
				end

				Character.Shirt.ShirtTemplate = MorphType.Shirt.ShirtTemplate
				Character.Pants.PantsTemplate = MorphType.Pants.PantsTemplate

				Weld(Character, MorphType)

				MorphEquipped = true
			end	
		end
		
		Remote.OnServerEvent:Connect(Equip)
		
	else
		for _, Morphs in ipairs(workspace.Morphs:GetChildren()) do 
			for _, EquipMorphButton in ipairs(Morphs:GetChildren()) do 
				if EquipMorphButton.Name == "EquipMorph" then
					EquipMorphButton.ProximityPrompt.Triggered:Connect(function()
						print(EquipMorphButton.Parent)
						Remote:FireServer(EquipMorphButton.Parent)
						return
					end)
				end
			end
		end
	end
end

return Morph

you are connecting to a function named “Equip” but theres no function named as that, if you are not gonna connect a function and create your own function you need to use

Remote.OnServerEvent:Connect(function(Player,Equip)

I connected it to this function

Where are you calling Morph.Init? If you are calling from a client then .OnServerEvent() will not work.

where arre you calling Morph.Init, i can’t see it

Maybe, you are getting this problem because the “MorphType” (or EquipMorphButton.Parent) only exists on the client. You could create a table which includes the info of the morph.

Example:

local NewMorph = EquipMorphButton.Parent
local MorphInfo = {
	Shirt = {
		ShirtTemplate = NewMorph.Shirt.ShirtTemplate
	};
	Pants = {
		PantsTemplate = NewMorph.Pants.PantsTemplate
	};
}
Script
local RunService = game:GetService("RunService")
local Remote = script.Remotes:WaitForChild("EquipMorph")

local MorphEquipped = false


local Morph = {}

local function Weld(Character, MorphType)
end

function Morph.Init()	
	if RunService:IsServer() then

		local function Equip(Player, MorphType)
			local Character = Player.Character
			local Humanoid = Character:FindFirstChild("Humanoid")

			if not MorphEquipped then 

				for _, Hats in ipairs(Character:GetChildren()) do 
					if Hats:IsA("Accessory") then 
						Hats:Destroy()
					end
				end

				Character.Shirt.ShirtTemplate = MorphType.Shirt.ShirtTemplate
				Character.Pants.PantsTemplate = MorphType.Pants.PantsTemplate

				Weld(Character, MorphType)

				MorphEquipped = true
			end	
		end

		Remote.OnServerEvent:Connect(Equip)

	else
		for _, Morphs in ipairs(workspace.Morphs:GetChildren()) do 
			for _, EquipMorphButton in ipairs(Morphs:GetChildren()) do 
				if EquipMorphButton.Name == "EquipMorph" then
					EquipMorphButton.ProximityPrompt.Triggered:Connect(function()
						local NewMorph = EquipMorphButton.Parent
						local MorphInfo = {
							Shirt = {
								ShirtTemplate = NewMorph.Shirt.ShirtTemplate
							};
							Pants = {
								PantsTemplate = NewMorph.Pants.PantsTemplate
							};
						}
						print(MorphInfo)
						Remote:FireServer(MorphInfo)
						return
					end)
				end
			end
		end
	end
end

return Morph

If this is not issue, are you calling Morph.Init from both client and server?

@FunAsiK @ProxySFX I have a module which requires all my modules and calls the .Init() in all of them:


self.Modules = {}

	if RunService:IsServer() then 
		for _, Module in next, ServerSciptService.ServerHandler.Modules:GetDescendants() do
			if Module:IsA("ModuleScript") then 
				self.Modules[Module.Name] = require(Module)
			end
		end
		
	else
		
		for _, Module in next, ReplicatedStorage.Modules:GetDescendants() do
			if Module:IsA("ModuleScript") and Module.Name ~= script.Name then 
				self.Modules[Module.Name] = require(Module)
			end
		end
	end
	
	local Success, Error = pcall(function()
		for _, Modules in pairs(self.Modules) do 
			Modules.Init()
		end
	end)

It still doesn’t work this this implemented.

Have you tried checking if there was anything inside the modules table?

Alright…so it prints nil… https://gyazo.com/566fd9681b5da80f1df9697414e37fd6

Why would it be nil though?

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerSciptService = game:GetService("ServerScriptService")

local Encyclopedia = {}
Encyclopedia.__index = Encyclopedia

function Encyclopedia.Initiate()
	local self = setmetatable({}, Encyclopedia)
	
	self.Modules = {}

	if RunService:IsServer() then 
		for _, Module in next, ServerSciptService.ServerHandler.Modules:GetDescendants() do
			if Module:IsA("ModuleScript") then 
				self.Modules[Module.Name] = require(Module)
			end
		end
		
	else
		
		for _, Module in next, ReplicatedStorage.Modules:GetDescendants() do
			if Module:IsA("ModuleScript") and Module.Name ~= script.Name then 
				self.Modules[Module.Name] = require(Module)
			end
		end
	end
	
	local Success, Error = pcall(function()
		for _, Modules in pairs(self.Modules) do 
			print(Modules.Name)
			Modules.Init()
		end
	end)
	
	if Error then
		warn(Error)
	end
	
	return self
end

return Encyclopedia

Entire script ^
I call .Initiate in both a local and server script

I meant to try printing the “Modules” itself. It is not an instance for it has no name, but it should be the required version of the item in self.Modules. So, you said that it printed nil. It’s printing nil because it’s not an instance, but I see that the modules were inserted into the table. I don’t see why it wouldn’t work.

you are saving value as [Module.Name] which means the key to that value will be Module.Name for example Module.Name is “someModule” so self.Modules["someModule"] will return that module

this had print nil because there is nothing saved for key named ["Name"]

https://gyazo.com/6bb47210ff5ccdab389bc4aad279fdce

I just thought of something. Maybe, the Morph.Init is running for the server and client at the same time, so the server doesn’t have enough time to connect the OnServerEvent before it is fired?

The screenshot says that both are running on the client.

That is true, if that is happening, how could I fix it?

Hmm… I don’t know. Where are you running the modules from? You could try to require and run the module on the server.

https://gyazo.com/62c591b2b42431f5a11047b2dabcc80e

In the Server and Client handler I have:

local Encyclopedia = require(game:GetService("ReplicatedStorage").Modules.Framework.Encyclopedia)
local EncyclopediaModule = Encyclopedia.Initiate()

Try waiting for a certain amount of time before executing on client. I’m not really sure why it would be running both on client though.