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

Why isn't load data working correctly?

Asked by 3 years ago

I have been working on a currency system for my game for a little bit trying to work out the issues. I finally got the save part of the script to work but now the load data part does not work correctly. When I run my test I start the game and press a button that takes away 100 Cash from the player, then I exit the game. The save print says the correct number in the console, however, when I join the game again the Cash value starts back at what it was the previous test. What am I missing here? (New to Lua, CompSci student)

local Players = game:GetService("Players")
local CashDataStore = game:GetService("DataStoreService"):GetDataStore("Cash") 

local STARTING_CASH = 100

local function onPlayerAdded(player)
    local playerKey = "Player_" .. player.UserId

    print("User ".. player.UserId.. " Joined")

    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

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

    local myCash
    local success, err = pcall(function()
        myCash = CashDataStore:GetAsync(playerKey) or STARTING_CASH
    end)
    if success then
        Cash.Value = myCash
        print("loading "..myCash)
    else
        print("failed to retrieve data")
    end

end

for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------

local function onPlayerLeft(player)
    local playerKey = "Player_"..player.UserId

    local savedCash = player.leaderstats.Cash.Value
    print("User has "..savedCash.." in the bank")
    savedCash = CashDataStore:SetAsync(playerKey, savedCash)
    print("User "..player.UserId.." disconnected")

end
Players.PlayerRemoving:Connect(onPlayerLeft)
0
are there any errors on the output? Shounak123 461 — 3y
0
There are no errors on the output. The last thing in the console is the last print that the save does. DaGlizzzzzzyyyy 34 — 3y
0
It will depend, do you change your cash value on the client, because that doesn't replicate on the server. Try changing it on the server and see if it works AnasBahauddin1978 715 — 3y
0
that could very well be the issue. I will check. DaGlizzzzzzyyyy 34 — 3y
0
I am using remote events to change the values server side. do I need to define the datastore in the RemoteEvents script as well? DaGlizzzzzzyyyy 34 — 3y

1 answer

Log in to vote
-1
Answered by 3 years ago

on line 31 - 33, there is no need to call the OnPlayerAdded() function for the players. The Players.PlayerAdded does everything required for it to work correctly. And I'm sure that's why the datastore doesn't work correctly.

So, what you need to do is just remove loop function at line 31, 32 and 33. It will be working fine then.

0
Trust me, there are no other flaws in your script. Shounak123 461 — 3y
0
every so often I get a message from the console saying "Disconnect from::ffff:numbers|numbers - Studio" not sure if I should give those numbers away. DaGlizzzzzzyyyy 34 — 3y
0
That OnPlayerAdded is run so that when people join before the PlayerAdded event is connected, it still runs it. There is not flaw with those lines AnasBahauddin1978 715 — 3y
0
It is impossible to join before playeradded runs. The lines are flaws. Shounak123 461 — 3y
0
It is impossible to join before playeradded runs. The lines are flaws. Shounak123 461 — 3y
Ad

Answer this question