Any reason my ModuleScript isn't working?

I have a ModuleScript that handles creating player stats and handling cash dropping for the player, which also edits the stats.

I used to have this in 2 separate scripts, but recently I tried merging them together using a ModuleScript and now nothing is working. Not even the test print I made to see if the script was running isn’t printing either.

This script is supposed to be in ServerScriptService, although I did try to move it to ReplicatedStorage, no luck. I’ve done everything I could to fix it but nothing worked. So please help me!

This is the main ModuleScript

local StatsProvider = {}

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local bankDataStore = DataStoreService:GetDataStore("PlayerBankData")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local cashDropTemplate = ReplicatedStorage:FindFirstChild("Storage") and ReplicatedStorage.Storage:FindFirstChild("CashDrop")
local recentCashDroppers = {}

print("hi") -- test to see if its running

local function SetupLeaderstats(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	print("Leaderstats folder created for", player.Name)

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats

	local bank = Instance.new("IntValue")
	bank.Name = "Bank"
	bank.Parent = leaderstats

	local success, savedBank = pcall(function()
		return bankDataStore:GetAsync(player.UserId)
	end)

	if success and savedBank then
		bank.Value = savedBank
	else
		bank.Value = 0
	end
end

local function CreateCashDrop(player, deathPosition)
	local leaderstats = player:FindFirstChild("leaderstats")
	local cashStat = leaderstats and leaderstats:FindFirstChild("Cash")
	if not cashStat or cashStat.Value <= 0 then
		return
	end

	local amountToDrop = math.max(1, math.floor(cashStat.Value * 0.02))
	cashStat.Value = cashStat.Value - amountToDrop

	table.insert(recentCashDroppers, player)

	if cashDropTemplate then
		local cashDrop = cashDropTemplate:Clone()
		cashDrop.Parent = workspace
		cashDrop.Position = deathPosition

		local amountValue = cashDrop:FindFirstChild("Amount")
		local cashGUI = cashDrop:FindFirstChild("CashGUI")
		local textLabel = cashGUI and cashGUI:FindFirstChild("Amount")

		if amountValue and textLabel then
			amountValue.Value = amountToDrop
			textLabel.Text = "$" .. tostring(amountToDrop)
		end

		local prompt = cashDrop:FindFirstChildOfClass("ProximityPrompt")
		if prompt then
			prompt.Triggered:Connect(function(triggeringPlayer)
				if table.find(recentCashDroppers, triggeringPlayer) then
					print(triggeringPlayer.Name .. " cannot pick up their own cash.")
					return
				end

				local triggerLeaderstats = triggeringPlayer:FindFirstChild("leaderstats")
				if triggerLeaderstats then
					local triggerCash = triggerLeaderstats:FindFirstChild("Cash")
					if triggerCash then
						triggerCash.Value = triggerCash.Value + amountToDrop
					end
				end
				cashDrop:Destroy()
			end)
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	print("Player added:", player.Name)
	SetupLeaderstats(player)

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()
			local deathPosition = character:FindFirstChild("HumanoidRootPart") and character.HumanoidRootPart.Position or Vector3.new(0, 0, 0)
			CreateCashDrop(player, deathPosition)
		end)
	end)
end)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		local success, err = pcall(function()
			bankDataStore:SetAsync(player.UserId, player.leaderstats.Bank.Value)
		end)
		if not success then
			warn("Failed to save bank data for player " .. player.Name .. ": " .. err)
		end
	end
end)

return StatsProvider

I apologize if the script is bad, I don’t really know what I’m doing with this.

1 Like

Hello, just to make sure, are you requiring the ModuleScript through a server Script? Since you said it’s not even printing the test, this might be because the ModuleScript isn’t being required (you have to use require() on a ModuleScript through a regular Script for it to run at all)

2 Likes

I second what dolphin asked/said, but I must add this:

You’re not using the module script correctly. Or at least, as it is currently, this could just be one script.

None of the functions or variables are stored within the ModuleScript table, making the ModuleScript itself pointless.

For now, I would suggest moving everything you have between the top line and bottom line into a script as it was working before you said as two scripts.

1 Like

I agree with all of these statements as well. There is one thing I am concerned about, however. I see data-saving logic for when the server shuts down, but no logic for when a client disconnects from the server. This would lead to every populated play session resulting in data-loss. Consider writing the following instead:

local function onPlayerRemoving(player: Player)
    -- Save data
end
Players.PlayerRemoving:Connect(onPlayerRemoving)

game:BindToClose(function()
    for _, player in Players:GetPlayers() do
        onPlayerRemoving(player)
    end
end)
4 Likes

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