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

DataStore saved my data as someone elses?

Asked by 6 years ago

So i went into the live server to test the game with my friend and we were playing for a bit, and then we got off.

We then got on later and we both had the same stats!

Not sure what is going on, if you want to look through my datastore setup here it is.

--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")

function savedata(dataname, playerid, value)
    game:GetService("DataStoreService"):GetDataStore(dataname):SetAsync(playerid, value)
end

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)

If you guys have any tips at all i will glady accept them, thanks all!

0
You should use a table instead of individual data stores, as it's more efficient Inconcinnus 90 — 6y

Answer this question