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

Why is my DataStore so inconsistent?

Asked by 6 years ago
Edited 6 years ago

Hey, so my datastore kind of sucks because it isn't consistent and it somehow will save my stats as someone elses sometimes, its really odd and i cant figure out why?

If you guys see anything that would make this not work please let me know, thank you!

--Variables
local leveldata = game:GetService("DataStoreService"):GetDataStore("level")
local EXPdata = game:GetService("DataStoreService"):GetDataStore("EXP")
local xpLeveldata = game:GetService("DataStoreService"):GetDataStore("xpLevel")
local Golddata = game:GetService("DataStoreService"):GetDataStore("Gold")
local toolsdata = game:GetService("DataStoreService"):GetDataStore("Tools")
local HttpService = game:GetService("HttpService")

level_gold = {"Gold", "Level"}
EXP_EXPNeeded = {"Exp" , "ExpNeeded"}

--Data Storage Save
function savedata(dataname, playerid, value)
    game:GetService("DataStoreService"):GetDataStore(dataname):setAsync(playerid, value)
end

--Leaderboard Variables
game.Players.PlayerAdded:Connect(function(player)
    playerKey = "user."..player.UserId  -- user key
    print(playerKey)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    --Gold
    local gold = Instance.new("IntValue")
    gold.Name = "Gold"
    gold.Value = Golddata:GetAsync(playerKey) or 0
    gold.Parent = leaderstats


    --Level
    local level = Instance.new("IntValue")
    level.Value = leveldata:GetAsync(playerKey) or 1
    level.Name = "Level"
    level.Parent = leaderstats



    --XP
    local EXP = Instance.new("IntValue")
    EXP.Value = EXPdata:GetAsync(playerKey) or 0
    EXP.Parent = player
    EXP.Name = "Exp"

    --XP Needed
    local xpLevel = Instance.new("IntValue")
    xpLevel.Value = xpLeveldata:GetAsync(playerKey) or 84
    xpLevel.Name = 'ExpNeeded'
    xpLevel.Parent = player

    --tools
    local tools = toolsdata:GetAsync(playerKey)
    if tools then
        for i,v in pairs(tools) do
            local tool = game.ReplicatedStorage.Tools:FindFirstChild(v)
            if tool then 
                tool:Clone().Parent = player.Backpack
                tool:Clone().Parent = player.StarterGear                    
            end
        end
    end

--Save data when player leaves
game.Players.PlayerRemoving:Connect(function(player) -- save player stats
    local toolsToSave = {}      
    for i,v in pairs(player.StarterGear:GetChildren()) do
        if v then
            table.insert(toolsToSave,v.Name)
        end
    end
    game:GetService("DataStoreService"):GetDataStore('Tools'):SetAsync(playerKey, toolsToSave)
    savedata('level',playerKey,player.leaderstats.Level.Value)
    savedata('EXP',playerKey,player.Exp.Value)
    savedata('xpLevel',playerKey,player.ExpNeeded.Value)
    savedata('Gold',playerKey,player.leaderstats.Gold.Value)
    print('data saved')
end)

I know i use alot of separate DataStores instead of putting them into a table, (mainly because i dont know how to put them into a table.) but do you think that would be a enough reason for this not to work??

Answer this question