Help with requesting function form module script

Im trying to request a function from the module script but its not working and im getting this error:

16:44:30.903 Workspace.Conveyor.Script:3: attempt to index nil with ‘SpawnRandomItem’ - Server - Script:3
16:44:30.904 Stack Begin - Studio
16:44:30.904 Script ‘Workspace.Conveyor.Script’, Line 3 - Studio - Script:3
16:44:30.904 Stack End - Studio
16:44:31.029 Players.TheReal_MrMonkey.PlayerScripts.LocalScript:22: Expected identifier when parsing expression, got ‘local’ - Studio - LocalScript:22
16:44:34.618 Disconnect from 127.0.0.1|58617 - Studio

Here is the script requesting the funciton

local module = unpack(require(game.ServerScriptService["Dough Spawning"]))
local SpawnLocation = script.Parent.SpawnPart
local SpawnDough = module.SpawnRandomItem()

while true do
	wait(0.2)
	SpawnDough(SpawnLocation)
end

why are you unpacking the module

i saw it in another fourm i thought it mighjt work it was orginally not unpacked

local module = require(game.ServerScriptService["Dough Spawning"])
local SpawnLocation = script.Parent.SpawnPart
local SpawnDough = module.SpawnRandomItem

while true do
	wait(0.2)
	SpawnDough(SpawnLocation)
end

its giving this error now

  17:28:45.408  Players.TheReal_MrMonkey.PlayerScripts.LocalScript:22: Expected identifier when parsing expression, got 'local'  -  Studio - LocalScript:22
  17:28:46.381  Workspace.Conveyor.Script:7: attempt to call a nil value  -  Server - Script:7
  17:28:46.381  Stack Begin  -  Studio
  17:28:46.381  Script 'Workspace.Conveyor.Script', Line 7  -  Studio - Script:7
  17:28:46.381  Stack End  -  Studio

actually im geeked the real problem is spawning the function, Workspace.Conveyor.Script:7: attempt to call a nil value

SpawnDough(SpawnLoaction)

here is the module script:

local SpawnModule = {} 

local ItemStorage = game.ReplicatedStorage.Dough -- Folder containing your items

local ItemChances = {
	-- ItemName = Weight
	["Chimpanzni"] = 50,
}

local LuckModifier = 1 

function SpawnRandomItem(SpawnLocation)
	local TotalWeight = 0
	for _, weight in pairs(ItemChances) do
		TotalWeight = TotalWeight + weight
	end

	local AdjustedTotalWeight = TotalWeight / LuckModifier 

	local RandomRoll = math.random(1,AdjustedTotalWeight)

	local CurrentWeight = 0
	local ChosenItemName = nil

	for ItemName, Weight in pairs(ItemChances) do
		CurrentWeight = CurrentWeight + Weight
		if RandomRoll <= CurrentWeight then
			ChosenItemName = ItemName
			break
		end
	end

	if ChosenItemName then
		local ItemToClone = ItemStorage:FindFirstChild(ChosenItemName)
		if ItemToClone then
			local ClonedItem = ItemToClone:Clone()
			ClonedItem.CFrame = SpawnLocation.CFrame
			ClonedItem.Parent = workspace
			print("Spawned: " .. ChosenItemName)
		end
	else
		warn("No item chosen!")
	end
end


return SpawnModule

You need to do function SpawnModule.SpawnRandomItem()
etc..

like this?

`local module = require(game.ServerScriptService[“Dough Spawning”])
local SpawnLocation = script.Parent.SpawnPart

while true do
wait(0.2)
module.SpawnRandomItem(SpawnLocation)
end`

because if so it does not work and says the same eroror

No not like that, in the module script do it.

function SpawnModule.SpawnRandomItem(SpawnLocation)
local TotalWeight = 0
for _, weight in pairs(ItemChances) do
TotalWeight = TotalWeight + weight
end

local AdjustedTotalWeight = TotalWeight / LuckModifier 

local RandomRoll = math.random(1,AdjustedTotalWeight)

local CurrentWeight = 0
local ChosenItemName = nil

for ItemName, Weight in pairs(ItemChances) do
	CurrentWeight = CurrentWeight + Weight
	if RandomRoll <= CurrentWeight then
		ChosenItemName = ItemName
		break
	end
end

if ChosenItemName then
	local ItemToClone = ItemStorage:FindFirstChild(ChosenItemName)
	if ItemToClone then
		local ClonedItem = ItemToClone:Clone()
		ClonedItem.CFrame = SpawnLocation.CFrame
		ClonedItem.Parent = workspace
		print("Spawned: " .. ChosenItemName)
	end
else
	warn("No item chosen!")
end

end

I dont know why it formatted it like that, but you see what todo.

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