Module script not working

  1. What do you want to achieve? A simple egg hatching system

  2. What is the issue? My module script wont run

I’m trying to create an egg hatching system but my module script won’t run there is no errors.

here is the Module script

local module = {}
local runservice = game:GetService('RunService')
local replicatedstorage = game:GetService('ReplicatedStorage')
local tweenservice = game:GetService('TweenService')
local Players = game:GetService('Players')

local eggs = workspace.Eggs
local remotes = replicatedstorage:WaitForChild('Remotes')
local pets = replicatedstorage:WaitForChild('Pets')

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local EggSystem = playerGui:WaitForChild('EggSystem')
local eggViewport = EggSystem:WaitForChild('ViewportFrame')


function module.HatchEgg(eggName, chosenPet)
	for i, v in pairs(playerGui:GetChildren()) do
		if v:IsA('ScreenGui') and v ~= EggSystem then
			v.Enabled = false
		end
	end
	eggViewport:ClearAllChildren()
	eggViewport.Size = UDim2.fromScale(0,0)

	local eggMesh = eggs:FindFirstChild(eggName):Clone()
	eggMesh.Parent = eggViewport
	eggMesh.CFrame = CFrame.new(0,0,0)

	local camera = Instance.new('Camera')
	camera.Parent = eggViewport

	camera.CFrame = CFrame.new(0,0,4)

	eggViewport.CurrentCamera = camera

	tweenservice:Create(eggViewport, TweenInfo.new(.6), {Size = UDim2.fromScale(.28,.627)}):Play()
end

remotes:WaitForChild('HatchEgg').OnClientEvent:Connect(module.HatchEgg)
return module

1 Like

why there is a onClientEvent on the module ?

module scripts is to give functions when you require them, the script wont run

1 Like

so i use a OnServerEvent? or do you want the script

this is the serverscript

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Players = game:GetService('Players')

local eggs = workspace.Eggs
local remotes = ReplicatedStorage:WaitForChild('Remotes')
local pets = ReplicatedStorage:WaitForChild('Pets')

local function chooseRandomPet(petTable)
	local chosenPet = nil
	local RadomNumber = math.random(1,100)
	local weight = 0
	
	for i,v in pairs(petTable) do
		weight += v.Chance
		
		if weight >= RadomNumber then
			chosenPet = v
			break
		end
	end
	return chosenPet
end

for _,egg in pairs(eggs:GetChildren()) do
	egg.ProximityPrompt.Triggered:Connect(function(player)
		local eggData = require(egg.Data)
		local price = eggData.EggPrice
		local Currency = eggData.EggCurrency
		
		if tonumber(player.leaderstats[Currency].Value) >= price then
			local chosenPet = chooseRandomPet(eggData.EggPets)
			player.leaderstats[Currency].Value -= price
			remotes:WaitForChild('HatchEgg'):FireClient(player, egg.Name, chosenPet)
		end
	end)
end

You must learn ModuleScripts first, ModuleScripts dont runs, in server or local, they are just to give functions to scripts

1 Like

and even you did not require the module script in server script

ModuleScripts cannot run code by itself, it can only return functions and execute those functions if require()ed by scripts.

You should put this line

remotes:WaitForChild('HatchEgg').OnClientEvent:Connect(module.HatchEgg)

into a script (LocalScript or Script).

1 Like

like what i said :confused:
char limit

1 Like