Problem with module script

I’m trying to make my module secure by putting it in ServerScriptService.

Well, the function I’m running never seems to work on both local and server script. I’ve tried a remote event, tried changing the function in the module script for local and/or server. I’ve even tried to swap the greater-than sign. Never worked.

It seems to be a problem with the module script, I’ve tried debugging it so mind the atrocious amount of print functions.

One main thing to note, I’ve tried this in a local script and the module was in replicated storage, still didn’t work at all and didn’t print anything. In the output for the server script it says this:


and prints nothing.

For local script it doesn’t print anything nor error.

Server script(first attempt):

local module = require(game:GetService("ServerScriptService"):WaitForChild("Buy"))

local test = workspace:WaitForChild("Test")

module.buy(test, 5)

Local script(second attempt)

local module = require(game:GetService("ReplicatedStorage"):WaitForChild("Buy"))

local test = workspace:WaitForChild("Test")

module.buy(test, 5)

Module script(for server script)

local buyitem = {}
print("Before event!")
game.Players.PlayerAdded:Connect(function(plr)
	print("Event called!")
	function buyitem.buy(item, itemcost)

		print("Creates a function.")
		local cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
		print("Gets the cash value!")
		if cash.Value >= itemcost then
			print("Compares the cost")
			cash.Value -= itemcost
			print("Subtracts cash!")
			local obj = Instance.new("ObjectValue")
			print("Creates")
			obj.Name = item.Name
			print("Names")
			obj.Value = item
			print("Sets Value")
			obj.Parent = plr.Inventory
			print(obj.Name)
			print("Parents")
			print("Done Debugging!")
		end
	end
end)

return buyitem

Module script(for local script):

local buyitem = {}
print("Before event!")
	print("Event called!")
	function buyitem.buy(item, itemcost)
	local plr = game.Players.LocalPlayer
		print("Creates a function.")
		local cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
		print("Gets the cash value!")
		if cash.Value >= itemcost then
			print("Compares the cost")
			cash.Value -= itemcost
			print("Subtracts cash!")
			local obj = Instance.new("ObjectValue")
			print("Creates")
			obj.Name = item.Name
			print("Names")
			obj.Value = item
			print("Sets Value")
			obj.Parent = plr.Inventory
			print(obj.Name)
			print("Parents")
			print("Done Debugging!")
		end
	end

return buyitem

I’m really wondering what’s gone wrong, and yes, I’ve swapped around the greater-than symbol, still no hope.

Can someone help me with this?

What line in the script did the error mention

The error mentioned line 3 of the server script.

Why does the function have an indent? shouldn’t it be like

local buyitem = {}
print("Before event!")
print("Event called!")
function buyitem.buy(item, itemcost)
	local plr = game.Players.LocalPlayer
	print("Creates a function.")
	local cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
	print("Gets the cash value!")
	if cash.Value >= itemcost then
		print("Compares the cost")
		cash.Value -= itemcost
		print("Subtracts cash!")
		local obj = Instance.new("ObjectValue")
		print("Creates")
		obj.Name = item.Name
		print("Names")
		obj.Value = item
		print("Sets Value")
		obj.Parent = plr.Inventory
		print(obj.Name)
		print("Parents")
		print("Done Debugging!")
	end
end

return buyitem

Apologizes, I’m not that good at formatting.

First of all, you absolutely need keep modules like this one on the server — exploiters can easily tamper with it if it’s on the client or in ReplicatedStorage.

Now with the main issue: remember that code can only run in a module script when it is called from the server. In the module, your buy function is in an event that will never run since events like PlayersAdded do not run in module scripts.

Try removing the PlayerAdded event and access the module from the server and see if the issue persists.

1 Like

I need the player function though, but I’ll try adding a parameter for it.

What’s the name of the module?

Hold on a second, I’ve completely forgot, how would I do this with a local script?

The module script’s name is Buy.

The script seems to be working for me, make sure all the names are correct

I just made a folder and number value inside named “leaderstats” and “Cash”, it works for me, no errors except for one at the very end

module in replicated storage:

local buyitem = {}
print("Before event!")
print("Event called!")
	function buyitem.buy(item, itemcost)
		local plr = game.Players.LocalPlayer
		print("Creates a function.")
		local cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
		print("Gets the cash value!")
		if cash.Value >= itemcost then
			print("Compares the cost")
			cash.Value -= itemcost
			print("Subtracts cash!")
			local obj = Instance.new("ObjectValue")
			print("Creates")
			obj.Name = item.Name
			print("Names")
			obj.Value = item
			print("Sets Value")
			obj.Parent = plr.Inventory
			print(obj.Name)
			print("Parents")
			print("Done Debugging!")
		end
	end

return buyitem

script in starter character:

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = game.Players.LocalPlayer

local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = leaderstats
cash.Value = 8

local module = require(game:GetService("ReplicatedStorage"):WaitForChild("Buy"))

local test = workspace:WaitForChild("Test")

module.buy(test, 5)

And I’m pretty sure “Inventory” is not a part of the player, I think it should be “Backpack”

My problem is accessing the module in ServerScriptService from a local script now.

Local scripts can’t access ServerScriptService or ServerStorage, only the server can

and like how @Friendly4Crafter said, PlayerAdded does not run in module scripts, so you’d have to find a way to get the player without using PlayerAdded

To access a server script from a local script, use a remote event. If you need a refresher on how remote events work, read this guide.

Tried that, maybe I’m just using it wrong as I’ve passed the entire service.

Instead maybe make a remote event that when the client fires, it sends only the item and the itemprice,

then, the remote event tells the server, it will then bring the player, item, and itemprice,

the server will then call the function in the module, and send another variable in the function which is the player, the module then just does all the other stuff

2 Likes

Right thanks for that, I’ll try it.

I’ll reply further if I have any issues or problems I face associated with this. Thanks!

1 Like