Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My cash won't save when i leave the game?

Whenever i give myself cash it's suppose to save but it won't

`local currencyName = "Cash"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		local savedData = DataStore:GetAsync(ID)
	end)
		
	if savedData ~= nil then
		currency.Value = savedData
		print("Data loaded")
	else
		currency.Value = 100
		print("Players to the game")
	end	
		
		
end)

game.Players.PlayerRemoving:Connect(function(player)
    local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	
	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down ok.")
		end
	end
	
	
	
	wait(5)
	
	
end)`

5 comments

0

Is this in Roblox Studio?

0

Are you giving you'reself cash through a code else it wont save beacuse you did it from the clients side and beacuse of fe it will think it is a exploit doing it. But i may be wrong

0

You should probably put this in a ModuleScript too inside of ServerStorage.

0

0k

2 answers

Try using these two scripts:

local MPS = game:GetService("MarketplaceService")
 
MPS.ProcessReceipt = function(receiptInfo)
    if receiptInfo.ProductId == 438167434 then -- replace with your ID here
        local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1000
        return Enum.ProductPurchaseDecision.PurchaseGranted
       
    elseif receiptInfo.ProductId == 443462976 then -- replace with your ID here
        local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 5000
        return Enum.ProductPurchaseDecision.PurchaseGranted
    
     elseif receiptInfo.ProductId ==443463248  then -- replace with your ID here
        local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 10000
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
end

This is just an example you can change the amount of money and money name

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local value = Instance.new("IntValue", folder)
    value.Name = "Cash"
    currency.Value = 1000 -- starting cash you can change this
end)

I hope this helps

Also put this in server storage and use a normal script.

2 comments

0

It didn't work

0

did you delete the other script you made

local currencyName = "Cash"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
local Players = game:GetService("Players")

function onPlayerJoined(Player)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = Player

    local currency = Instance.new("IntValue")
    currency.Name = currencyName
    currency.Parent = folder

    local ID = currencyName.."-"..player.UserId
    local savedData = nil

    pcall(function()
        local savedData = DataStore:GetAsync(ID)
    end)

    if savedData ~= nil then
        currency.Value = savedData
        print("Data loaded")
    else
        currency.Value = 100
        print("Players to the game")
    end 
end

function onPlayerLeft(Player)
    local ID = currencyName.."-"..Player.UserId
    DataStore:SetAsync(ID,Player.leaderstats:FindFirstChild(currencyName).Value)
end

game:BindToClose(function()
	for i,v in pairs(Players:GetPlayers()) do
		onPlayerLeft(v)
	end
end)

Players.PlayerAdded:Connect(onPlayerJoined)
Players.PlayerRemoving:Connect(onPlayerLeft)

4 comments

0

Its because you kicked them instead of saving their data. Try it now

0

nope it did not work

0

Well I redid some stuff but why are you trying to index leaderstats when it's not a table? Plus indexing should be using numbers not strings

0

idk i have not script for quite a long time and i'm actually referring my old scripts that i made

This discussion is preserved as it was. New answers, comments, and votes are closed.