Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)`
0
Is this in Roblox Studio? ShutokouBattle 227 — 4y
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 Freddan2006YT 88 — 4y
0
You should probably put this in a ModuleScript too inside of ServerStorage. BennyBoiOriginal 293 — 4y
0
0k Jadgpanthers2 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
It didn't work Jadgpanthers2 0 — 4y
0
did you delete the other script you made HiHowAreYah5 19 — 4y
Ad
Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
4 years ago
Edited 4 years ago
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)
0
Its because you kicked them instead of saving their data. Try it now Mr_Unlucky 1085 — 4y
0
nope it did not work Jadgpanthers2 0 — 4y
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 Mr_Unlucky 1085 — 4y
0
idk i have not script for quite a long time and i'm actually referring my old scripts that i made Jadgpanthers2 0 — 4y

Answer this question