Cash Saving Leaderstat Help

So I have a question, what is wrong with my code here -

Problem : I want to have two leaderstats Minutes and Cash

Minutes : Show on leaderboard
Cash : Is just stored in the player not on leaderboard

I already have the main script for saving and Minutes, ATM This is working :

	@author TwinPlayzDev_YT
	@since 1/28/2021
	This script will save and put minutes on the leaderboard. You can change minutes to anything--
	you would like just simply read the code and look at Minutes.Name = "Minutes" change "Minutes"
	to anything like "Points", "Level", etc. Please watch my youtube tutorial if needed.
--]]


--[ SERVICES ]--

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
local service = game:GetService("MarketplaceService")

--[ LOCALS ]--

local VIPGamepassId = 13913938  -- VIP GAMEPASS ID
local GamepassId = 13914120  -- GAMEPASS ID

--[ FUNCTIONS ]--

game.Players.PlayerAdded:Connect(function(Player)
	
    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
    local Minutes = Instance.new("IntValue")
    Minutes.Name = "Minutes" -- changing name here (points, levels, time, etc.)
    Minutes.Value = 0 -- default value
	Minutes.Parent = Leaderstats
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash" -- changing name here (points, levels, time, etc.)
	Cash.Value = 0 -- default value
	Cash.Parent = Player
    
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
		Minutes.Value = Data
		Cash.Value = Data
    end
	
	local incrementValue = 1 -- value when adding points
	
	if (service:UserOwnsGamePassAsync(Player.UserId, VIPGamepassId)) then -- 3x gamepass
		incrementValue = 3
	elseif (service:UserOwnsGamePassAsync(Player.UserId, GamepassId)) then -- 2x gamepass
		incrementValue = 2
	end
	
	coroutine.resume(coroutine.create(function() -- gives 1 point every minute
        while true do
            wait(60)
			Minutes.Value = Minutes.Value + incrementValue --  adds points based off of the incrementValue
			Cash.Value = Cash.Value + incrementValue
        end
	end))
end)

game.Players.PlayerRemoving:Connect(function(Player) -- save function here
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
	DataStore:SetAsync(Player.UserId, Player.Cash.Value)
end)

The only problem is that my leaderstat’s “Cash” is combining with “Minutes” I’m guessing because I’m storing it in the same place?

Like my Cash is The Amount Of Minutes.

Im trying to achieve a Cash Money Gui Like Such -
cash gui

But The 0 is turning into - The Minutes Leaderstat.

Let me know what’s wrong in my script.

1 Like

You would have to save cash and minutes in a table.

Well the thing is Minutes is already working, so don’t I just have to make something like another DataStore for Cash?

I mean Im not trying to reset Minutes.

You can save them in the same datastore like this

local Data = DataStore:GetAsync(Player.UserId) -- Get data
if Data then
    Minutes.Value = Data.Minutes
    Cash.Value = Data.Cash
end

game.Players.PlayerRemoving:Connect(function(Player) -- save data
	DataStore:SetAsync(Player.UserId, {
        Minutes = Player.leaderstats.Minutes.Value,
        Cash = Player.Cash.Value
    })
end)
1 Like

That’s because you’re parenting cash to the player, and not to the leaderstats folder, ignore what he said about tables, it becomes more annoying to convert your datastores to use tables later. It’s good to have one datastore entry for the maximum possible, but if you’re learning that shouldn’t be a concern.

1 Like