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

player data is not being saved?

Asked by 4 years ago

i'm trying to learn about data store and have copied this script exactly the way it is from the youtube video I'm watching. enable studio access to api servers is on. after I change the value of Cash when I'm testing in the server nothing happens. no errors either

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


--Player Joins

game.Players.PlayerAdded:Connect(function(player)

    --Gives Player Stats

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

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

    local playerUserId = "Player_"..player.UserId



    --Load Data

    local data

    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)

    if success then
        Cash.Value = data
    end
end)

-- Save Data

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = player.leaderstats.Cash.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

    if success then
        print("Data Saved")
    else
        print("error")
        warn(errormessage)
    end

end)

2 answers

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

Hello, i'm gonna try to help you but first save your actual script because i will make you modify it a lot but i'm pretty sure it will work!

Make a line ontop for your script like this: local cash = game:GetService("DataStoreService"):GetDataStore("CASH") And then after that line do: function savedata(dataname, playerid, value) game:GetService("DataStoreService"):GetDataStore(dataname):SetAsync(playerid, value) end

game.Players.PlayerAdded:Connect(function(player) So now make a function to detect when the cash value changes, it would be something like this: Cash.Changed:Connect(function() And why do you want to detect when the cash changes? to save data every time the cash value changes. How you do that? Simple, do this: savedata("CASH",player.userId,Cash.Value) Do that right after the changed function. So now you will save data when the player leaves too just to be sure it's gonna save data 100%. Do this after PlayerRemoving: savedata("CASH",player.userId,player.leaderstats.Cash.Value) And done, your money will be saved every time you get cash and leave the game! Also you need to replace local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") and local playerUserId = "Player_"..player.UserId

    --Load Data

local data

    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)

    if success then
        Cash.Value = data

end end) Because you don't need them. IMPORTANT: GIVE THE CASH A VALUE LIKE 0 OR IT WONT WORK! And delete everything after PlayerRemoving too and replace it with what i said.

0
The first end goes below game:GetService("DataStoreService"):GetDataStore(dataname):SetAsync(playerid, value). LetalNightmare 99 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Try using :BindToClose. If you're the only one in the server and you leave, the server may shut down first and not allow all your functions to finish running. The server can also shut down unexpectedly even if there is more than 1 player. :BindToClose will allow bound functions to run 30 seconds before the server shuts down. You can just add this at the end of your script.

game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do -- looping through the players will allow this function to run for every player in the server
        local playerUserId = "Player_"..player.UserId

        local data = player.leaderstats.Cash.Value

            local success, errormessage = pcall(function()
                myDataStore:SetAsync(playerUserId, data)
            end)

        if success then
            print("Data Saved")
        else
                print("error")
                warn(errormessage)
        end
    end
end)

Answer this question