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

Can anyone help? Datastore script seems to add a level every time the player leaves and rejoins.

Asked by 5 years ago
Edited 5 years ago

Yo, so my datastore script seems to have this issue where instead of saving the players level and exp, it for some reason jumps a level or two sometimes?? (for example: the players level would be 1, then once they leave and rejoin it becomes 2?)

Here is the code if anyone wants to know;

local DSService = game:GetService("DataStoreService"):GetDataStore("-test#1222")
local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(plr)
    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new("Folder", plr)
    local Level = Instance.new("NumberValue")
    local EXP = Instance.new("NumberValue")
    local MaxEXP = Instance.new("NumberValue")
    local Cash = Instance.new("NumberValue")
    local FactionEXP = Instance.new("NumberValue")

        local Strength = Instance.new("NumberValue")
        local Stamina = Instance.new("NumberValue")
        local Defence = Instance.new("NumberValue")
        local Quirk = Instance.new("NumberValue")
        local Points = Instance.new("NumberValue")

    local New =  Instance.new("BoolValue")

    leaderstats.Name = "Data"

    Level.Parent = leaderstats
    EXP.Parent = leaderstats
    MaxEXP.Parent = leaderstats
    Cash.Parent = leaderstats
    FactionEXP.Parent = leaderstats
    New.Parent = leaderstats

    Strength.Parent = leaderstats
    Stamina.Parent = leaderstats
    Defence.Parent = leaderstats
    Quirk.Parent = leaderstats
    Points.Parent = leaderstats

    Level.Name = "Level"
    EXP.Name = "EXP"
    MaxEXP.Name = "MaxEXP"
    Cash.Name = "Cash"
    FactionEXP.Name = "FactionEXP"

    Strength.Name = "Strength"
    Stamina.Name = "Stamina"
    Defence.Name = "Defence"
    Quirk.Name = "Quirk"
    Points.Name = "Points"

    New.Name = "New"

    Level.Value = 1
    EXP.Value = 0
    MaxEXP.Value = 100
    Cash.Value = 0
    FactionEXP.Value = 0
    New.Value = true

    Strength.Value = 0
    Stamina.Value = 0
    Defence.Value = 0
    Quirk.Value = 0
    Points.Value = 0


    local GetSaved = DSService:GetAsync(uniquekey)

    if GetSaved then
        Level.Value = GetSaved[1]
        EXP.Value = GetSaved[2]
        Cash.Value = GetSaved[3]
        FactionEXP.Value = GetSaved[4]
        New.Value = GetSaved[5]

            Strength.Value = GetSaved[6]
            Stamina.Value = GetSaved[7]
            Defence.Value = GetSaved[8]
            Quirk.Value = GetSaved[9]
            EXP.Value = GetSaved[10]
            Points.Value = GetSaved[11]
    else
        local NumberFS = {Level.Value,EXP.Value,Cash.Value,FactionEXP.Value,New.Value,Strength.Value,Defence.Value,Quirk.Value,Stamina.Value,MaxEXP.Value,Points.Value,}
        DSService:SetAsync(uniquekey,NumberFS)
    end


end)






game.Players.PlayerRemoving:Connect(function(plr)
    local uniquekey = "id-"..plr.userId
    local SaveTable = {plr.Data.Level.Value,plr.Data.EXP.Value,plr.Data.Cash.Value,plr.Data.FactionEXP.Value,plr.Data.New.Value,plr.Data.Strength.Value,plr.Data.Defence.Value,plr.Data.Stamina.Value,plr.Data.Quirk.Value,plr.Data.MaxEXP.Value,plr.Data.Points.Value}
    DSService:SetAsync(uniquekey, SaveTable)
    print("Number")
end)~~~~~~~~~~~~~~~~~

0
Contact me via discord and I might be able to help you in another way Mystifine#4924 BlackOrange3343 2676 — 5y

1 answer

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

Heres my datastore:

local Ds = game:GetService("DataStoreService") -- access datastore
local abilites = Ds:GetDataStore("PlayerStats_00") -- Datastore key

function getPlayerData(player) -- load data
    local success, data = false, nil
    repeat
        success = pcall(function()
            data = abilites:GetAsync(player.UserId)
        end)
    until success

    player.IfData.Value = true

    return data
end

function saveData(player)
    if player.IfData.Value then
        local data = {}
        for i,v in pairs(player.leaderstats:GetChildren()) do
            table.insert(data, v.Value)
        end
        pcall(function()
            abilites:SetAsync(player.UserId, data)
        end)
    end
end

function loadStats(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

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

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

    local loaded = Instance.new("BoolValue", player)
    loaded.Name = "IfData"  

    local savefile = getPlayerData(player)
    if savefile then
        Credits.Value = savefile[1]
        Wins.Value = savefile[1]
    else
        Credits.Value = 0
        Wins.Value = 0
    end
end

game.Players.PlayerAdded:Connect(loadStats) -- When player joins load their stats
game.Players.PlayerRemoving:Connect(saveData) -- when player leaves save their stats

-- Auto-Saving (When game is releasing make an setting)
while wait(30) do
    for _, player in pairs(game.Players:GetPlayers())do
        saveData(player)
    end
end
0
that didnt seem to fix the problem? any other ideas? Busoshoaku 0 — 5y
0
Maybe I could show you my datastore its kind of similar XxWingKing 96 — 5y
0
alright, that would be really helpful since i could just edit the values Busoshoaku 0 — 5y
0
I have edited my answer XxWingKing 96 — 5y
View all comments (3 more)
0
thanks, im trying it now Busoshoaku 0 — 5y
0
thank you so much! it seems to work fine, if i could upvote you i would because you've spent alot of time trying to help me make this, so thanks alot. Busoshoaku 0 — 5y
0
Could you mark me as the answer XxWingKing 96 — 5y
Ad

Answer this question