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

how do you make a player data save system?

Asked by
Kaput_0 44
4 years ago

i am working on a system that saves player data when a player leaves or joins, but it doesn't seems to be working, but I not getting any errors and it says its saving properly, how do I fix it?

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

local player
local playerid
local usertable

game.Players.PlayerAdded:Connect(function(plr)
    player = plr.Name
    playerid = plr.UserId

    usertable = 'player_' .. playerid

    local success , currentlevel = pcall(function()
        return Stage:GetAsync(usertable)
    end)

    if success then
        game.ReplicatedFirst.Level.Value = Stage
        print('data loaded:' .. game.ReplicatedFirst.Level.Value)
    else
        print('ok that dident work')
    end
end)

game.Players.PlayerRemoving:Connect(function()
    local success , Stage = pcall(function()
        Stage:SetAsync(usertable,game.ReplicatedFirst.Level.Value)
    end)

    if success then
        print('data saved: ' .. game.ReplicatedFirst.Level.Value)
    end
end)
0
local breathingName = "HaveBreathing" local birdName = "HaveBird" local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore") game.Players.PlayerAdded:Connect(function(player) local folder = Instance.new("Folder") folder.Name = "bool" folder.Parent = player local havebird = Instance.new("IntValue") havebird.Name = birdName havebird.Parent = folder local havebrea TheGreenGaming886 2 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
local breathingName = "HaveBreathing"
local birdName = "HaveBird"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")

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

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

    local havebird = Instance.new("IntValue")
    havebird.Name = birdName
    havebird.Parent = folder

    local havebreathing = Instance.new("IntValue")
    havebreathing.Name = breathingName
    havebreathing.Parent = folder

    local ID2 = breathingName.."-"..player.UserId
    local ID1 = birdName.."-"..player.UserId

    local savedData = nil

    pcall(function()
        savedData = DataStore:GetAsync(ID2)
        savedData = DataStore:GetAsync(ID1)
    end)
    if savedData ~= nil then
        havebird.Value = savedData
        havebreathing.Value = savedData
        print("Data loaded")
    else
        havebreathing.Value = 0
        havebird.Value = 0
        print("New player to the game")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local ID2 = breathingName.."-"..player.UserId
    local ID1 = birdName.."-"..player.UserId
    DataStore:SetAsync(ID1,player.bool[breathingName].Value)
    DataStore:SetAsync(ID2,player.bool[birdName].Value)
end)

game:BindToClose(function()

    -- When game is ready to shutdown

    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("This game is shutting down")
        end
    end

    wait(5) 

end)

0
what exactly am i replacing here? Kaput_0 44 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I personally write all my DataStores in a dictionary format.

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("Stage")

local playerData = {}

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

    local key = plr.UserId

    playerData[plr.Name] = {}
    playerData[plr.Name].Stage = 0

    local getData 
    local succ, err = pcall(function()
        getData = ds:GetAsync(key)
    end)

    if succ then
        if getData then
            print("data successfully loaded")
            playerData[plr.Name] = getData
        else
            playerData[plr.Name].Stage = 0
            print("no save")
        end
    else
        warn(err)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local key = plr.UserId
    local succ, err = pcall(function()
        ds:SetAsync(key, playerData[plr.Name])
    end)
    if succ then
        print("save successful")
        playerData[plr.Name] = nil
    end
end)
0
it seems good but how would i change the data value? Kaput_0 44 — 4y
0
Do a quick FireServer(), validate if everything seems true and alright, then run playerData[plr.Name].Stage = playerData[plr.Name].Stage + 1 CalvinTLincoln 2 — 4y

Answer this question