Harl23
Harl23

Reputation: 27

Lua - How can I make player money save?

I have been tinkering with this issue for a good little bit today and just can't seem to figure out what is happening. I want my roblox game to save the money of a character when they leave the game, and reload that same value when they join back. The problem is, well... that is not happening. Every time I re-join, my money is 0 even though I set it to 100. Here is my leaderstats script:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Test")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Money"

    local playerKey = "id_"..player.UserId
    local GetSaved = dataStore:GetAsync(playerKey)
    money.Value = GetSaved and GetSaved[1] or 0 
end)

and here is my save script:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Test")

game.Players.PlayerAdded:Connect(function(player)
    local playerKey = "id_"..player.UserId
    local saveValue = player:WaitForChild("leaderstats"):WaitForChild("Money")
    
    local GetSaved = dataStore:GetAsync(playerKey)
    if GetSaved then
        saveValue.Value = GetSaved[1]
    else
        local NumbersForSaving = {saveValue.Value}
        dataStore:GetAsync(playerKey, NumbersForSaving)
    end
    wait()  
end)

game.Players.PlayerRemoving:Connect(function(player)
    dataStore:SetAsync("id_"..player.UserId, {player.leaderstats["Money"].Value}) 
end)

I am not very familiar with Lua and would greatly appreciate if anyone could look this over and point something out that I may be missing. Thanks!

Upvotes: 1

Views: 187

Answers (1)

Darrian Penman
Darrian Penman

Reputation: 123

Datastores can be a pain at first but once you figure them out and how to do it, it becomes very easy. When I first tried using them I had similar problems, I found that it's really picky with how you do the transactions between the game and datastores.

The simplest and easiest way to set it up is to do your leaderstats like this:

-- Data Store Service:
local dataStoreService = game:GetService("DataStoreService")
-- The specific Data store you're using (String IDs):
local moneyStore = dataStoreService:GetDataStore("money")
-- Players Service:
local players = game:GetService("Players")

-- Usual leaderstats setup:
players.PlayerAdded:Connect(function(player)

    -- Leaderstats: 
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    -- Make Money Leaderstat:
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Parent = leaderstats
    -- Sets the leaderstat to the current datastore value, 0 if not found.
    money.Value = moneyStore:GetAsync(player.UserId) or 0
    -- Binds the PlayerRemoving event to saving the leaderstat into the datastore
    game.Players.PlayerRemoving:Connect(function(player)
        moneyStore:SetAsync(player.UserId, money.Value)
    end)
end)

This is the simplest method of using DataStores, its far from perfect due to a few reasons. Relying on leaderstat values can put your game at risk of exploiters as leaderstats are well documented and targeted values in ROBLOX games for exploits. Making the leaderstat value a "Display Only" value would help, hide the actual value somewhere else. Alternatively you can save the value every time it's changed to the data store straight away.

When changing the value always make sure you're doing it in a Server Side Script and if its a Local Script that you use Remote Events to make the server do it.

If you would like to test this method of using DataStores, put this server script in a random part and touch it and leave the game and join back to make sure it saved.

script.Parent.Touched:Connect(function(toucher)
    local humanoid = toucher.Parent:FindFirstChild("Humanoid")
    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(toucher.Parent)
        player.leaderstats.Money.Value = player.leaderstats.Money.Value + 10
    end
end)

Additionally make sure the game you're attempting to use DataStores has this option enabled: Enable Studio Access to API Services

enter image description here

This setting can be found under: FILE/Game Settings/Security

I hope this helps!

Upvotes: 0

Related Questions