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.