Why is my "money" value desapearing

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a money system NOT in a database because i don’t want player to see other’s money and i want other player’s to be able to acces the money of other’s

  2. What is the issue? Include screenshots / videos if possible!
    I have a part of the script wich add’s money to a player and then print’s it and another part wich just reads it and then print’s it. But when the second part try’s to read it, it print’s nil and i can’T figure out why.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have search on the devforum and found other peaple doinf tables in tables like me but can’t figure out why my thing is not working

Here is my code

local module = {}



module.playerMOney = function(plr, addremove, amount, newplayer)
	local MoneyModule = require(game.ReplicatedStorage.Money)
	local sarterMoney = 500
	local playerMoneyTable = {}
	local plrMoney
	
	if addremove == nil then
		if plr == nil then
			if newplayer == nil then
			else
				playerMoneyTable[newplayer.name] = sarterMoney
				print(playerMoneyTable["simsim26102006"])
			end
		else
			plrMoney = playerMoneyTable[plr.name]
			print(playerMoneyTable[plr.name])
		end
	else
		if addremove == "add" then
			print(playerMoneyTable["simsim26102006"])
		elseif addremove == "remove" then
		end
	end
	return(plrMoney)
end

return module

And here is my output :
17:08:15.927 500 - Server - Money:16
17:08:20.931 nil - Server - Money:26

2 Likes

Also here is the script that fire all of that:

game.Players.PlayerAdded:Connect(function(plr)

	local MoneyModule = require(game.ReplicatedStorage.Money)
	MoneyModule.playerMOney(nil, nil, nil, plr)
	wait(5)
	MoneyModule.playerMOney(plr, "add", 50, nil)
end)
1 Like

This right here. Every time the function is called it makes a new table, then when the function is done you have no way to access that table after the function has finished running because the reference is inside the function, so I believe garbage collection is just gonna scoop it up later. You need to put it somewhere where you can access the table again, like outside of that function.

Something like this

local playerMoneyTable = {}
module.playerMOney = function(plr, addremove, amount, newplayer)
	local MoneyModule = require(game.ReplicatedStorage.Money)
	local sarterMoney = 500
	local plrMoney
	
	if addremove == nil then
		if plr == nil then
			if newplayer == nil then
			else
				playerMoneyTable[newplayer.name] = sarterMoney
				print(playerMoneyTable["simsim26102006"])
			end
		else
			plrMoney = playerMoneyTable[plr.name]
			print(playerMoneyTable[plr.name])
		end
	else
		if addremove == "add" then
			print(playerMoneyTable["simsim26102006"])
		elseif addremove == "remove" then
		end
	end
	return(plrMoney)
end

Put it on the outside so you will have access to it. Once the function is done running, you won’t have access to any infomation you created in it unless you store it somewhere outside of the function.

----------------------------------------------------------------------------------------------------------------
here try this this will work much better
----------------------------------------------------------------------------------------------------------------
local module = {}
module .__index = module 

local sarterMoney = 500
local playersMoney = {}


function module:playerMOney(plr, amount)
	
	if not playersMoney[plr.name] then
		playersMoney[plr.name] = sarterMoney
	end
	
	if amount then
		playersMoney[plr.name] += amount
		if playersMoney[plr.name] < 0 then
			playersMoney[plr.name] = 0
		end
	end
	
	return(playersMoney[plr.name])
end


return module 

----------------------------------------------------------------------------------------------------------------
game.Players.PlayerAdded:Connect(function(plr)

	local RS = game:GetService("ReplicatedStorage")
	local MoneyModule = require(RS.Money)
	
	MoneyModule:playerMOney(plr, 50) -- To increase the player's money MoneyModule:playerMOney(player, amount)
	wait(5)
	MoneyModule.playerMOney(plr, -25) -- To reduce the player's money MoneyModule:playerMOney(player, -amount) -- just put a minus before amount
end)
----------------------------------------------------------------------------------------------------------------
1 Like

Here is my code
It almost worked, i just needed to remove the .name to every plr

so like this

local module = {}
module .__index = module 

local sarterMoney = 500
local playersMoney = {}


function module:playerMOney(plr, amount)

	if not playersMoney[plr] then
		playersMoney[plr] = sarterMoney
	end

	if amount then
		playersMoney[plr] += amount
		if playersMoney[plr] < 0 then
			playersMoney[plr] = 0
		end
	end

	return(playersMoney[plr])
end


return module 
game.Players.PlayerAdded:Connect(function(plr)

	local RS = game:GetService("ReplicatedStorage")
	local MoneyModule = require(RS.Money)

	print(MoneyModule:playerMOney(plr, 50))-- To increase the player's money MoneyModule:playerMOney(player, amount)
	wait(5)
	print(MoneyModule.playerMOney(plr, -25)) -- To reduce the player's money MoneyModule:playerMOney(player, -amount) -- just put a minus before amount
end)
1 Like

Undeeded metamethod module.__index = module when you’re not inheriting anything, no constructors. Clean up the code a bit so it’s easier to read because at the moment there are references left right centre. Add some variables in there man. Add comments to understand it later and please, be consistent with your naming conventions.