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

Data Load loading only one value and giving 0 value to the others?

Asked by 5 years ago
Edited 5 years ago

Alright, after a lot of time and hard work I got how to do a decent load / save data script. 1st Try.

local DataStore = game:GetService("DataStoreService"):GetDataStore("StoreName")

game.Players.PlayerAdded:connect(function(player)
    local key = "key-"..player.userId
    local points = game.ReplicatedStorage.AvailablePoints
    local strength = game.ReplicatedStorage.Strength
    local level = game.ReplicatedStorage.Level
    local quirk = game.ReplicatedStorage.Quirk

    local save = DataStore:GetAsync(key)
    if save then 
        points.Value = save[1]
        strength.Value = save[2]
        level.Value = save[3]
        quirk.Value = save[4]
    else
        local load = {points.Value, strength.Value, quirk.Value, level.Value}
        DataStore:SetAsync(key,load)
    end
end)


game.Players.PlayerRemoving:connect(function(player)
    local key = "key-"..player.userId
    local load = {game.ReplicatedStorage:FindFirstChild("AvailablePoints").Value, game.ReplicatedStorage:FindFirstChild("Quirk").Value, game.ReplicatedStorage:FindFirstChild("Level").Value, game.ReplicatedStorage:FindFirstChild("Strength").Value}
    DataStore:SetAsync(key,load)

end)

And it actually worked! but, the real problem now is. It only loads me the points value, and not also strength, quirk and level. Can you find any error in here? I also tried to give myself more Points with a script and tried different times to give me points in Strength since only AvailablePoints loaded and not the others!

EDIT: This is the actual script that keeps not working.

local DataStore = game:GetService("DataStoreService")
local ST = DataStore:GetDataStore("StoreName")

game.Players.PlayerAdded:Connect(function(player)
    local Backpack = player:WaitForChild("Backpack")
    local points = Backpack.AvailablePoints
    local strength = Backpack.Strength
    local level = Backpack.Level
    local quirk = Backpack.Quirk
    points.Value = ST:GetAsync(player.UserId) or 1
    strength.Value = ST:GetAsync(player.UserId) or 2
    level.Value = ST:GetAsync(player.UserId) or 3
    quirk.Value = ST:GetAsync(player.UserId) or 4

    while wait(7) do
        ST:SetAsync(player.UserId, points.Value, strength.Value, level.Value, quirk.Value)
        print("works!")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Backpack = player:WaitForChild("Backpack")
    ST:SetAsync(player.UserId, Backpack.AvailablePoints.Value, Backpack.Strength.Value, Backpack.Level.Value, Backpack.Quirk.Value)
    print("works!")
end)
0
UserId not userId. Connect not connect. User#19524 175 — 5y
0
Still not working. Fixer1987 95 — 5y
0
It may be because you're trying to load the first value of the saved table in your GetAsync for all of your values. Optimalpokemon123 37 — 5y
0
I tried, and it's not working, but that was an error too! Fixer1987 95 — 5y
View all comments (3 more)
0
On lines 12-15, try changing the values to go accordingly 1,2,3,4. Let me know if that worked but I am a novice. BunnyFilms1 297 — 5y
0
Still not working :c Fixer1987 95 — 5y
0
All I can really suggest is an article on data stores: https://scriptinghelpers.org/guides/saving-and-loading-data-with-datastores BunnyFilms1 297 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

Alright so i rewrote your script and to my knowledge it work, contact me if this does not work

local DataStore = game:GetService("DataStoreService")
local ST = DataStore:GetDataStore("StoreName")

game.Players.PlayerAdded:Connect(function(player)
    local points = game.ReplicatedStorage.AvaliablePoints
    points.Value = ST:GetAsync(player.UserId) or 1
    --local strength = game.ReplicatedStorage.Strength
    --local level = game.ReplicatedStorage.Level
    --local quirk = game.ReplicatedStorage.Quirk

    points.Changed:Connect(function()
        ST:SetAsync(player.UserId, points.Value)
        print("works!")
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    ST:SetAsync(player.UserId, game.ReplicatedStorage.AvaliablePoints.Value)
    print("works!")
end)

So do the same thing i did to the points to all the rest of the values you have. Be sure to also turn on API serviced in the game settings

Hope this helps, Father_Odin

0
you can also change the "or 1" to whatever number you want the default to be Father_Odin 2 — 5y
0
please bare with my typing skills to xD Father_Odin 2 — 5y
0
local DataStore = game:GetService("DataStoreService") local ST = DataStore:GetDataStore("StoreName") game.Players.PlayerAdded:Connect(function(player) local Backpack = player:WaitForChild("Backpack") local points = Backpack.AvailablePoints local strength = Backpack.Strength local level = Backpack.Level local quirk = Backpack.Quirk points.Value = ST:GetAsync(player.UserId) or Fixer1987 95 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I've tried this, but still not working. It saves everything, but when I come in game, Strength value is wrong, but Available points is correct. Why?

local DataStore = game:GetService("DataStoreService")
local ST = DataStore:GetDataStore("StoreName")

game.Players.PlayerAdded:Connect(function(player)
    local Backpack = player:WaitForChild("Backpack")
    local points = Backpack.AvailablePoints
    local strength = Backpack.Strength
    local level = Backpack.Level
    local quirk = Backpack.Quirk
    points.Value = ST:GetAsync(player.UserId) or 1
    strength.Value = ST:GetAsync(player.UserId) or 2
    level.Value = ST:GetAsync(player.UserId) or 3
    quirk.Value = ST:GetAsync(player.UserId) or 4

    while wait(7) do
        ST:SetAsync(player.UserId, points.Value, strength.Value, level.Value, quirk.Value)
        print("works!")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Backpack = player:WaitForChild("Backpack")
    ST:SetAsync(player.UserId, Backpack.AvailablePoints.Value, Backpack.Strength.Value, Backpack.Level.Value, Backpack.Quirk.Value)
    print("works!")
end)

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

You want to be creating new stats per player but in your code you use the same value holders to store stats meaning you are mixing player data.

To simplify thing you can create them when the plauer joins using a simple function to make the instance.

Example:-

-- create a new instance with the data passed
local function mkStat(statType, name, val)
    local tmp = Instance.new(statType)
    tmp.Name = name

    if val ~= nil then -- set value if one is passed
        tmp.Value = val
    end

    return tmp
end

game.Players.PlayerAdded:Connect(function(plr)
    -- player stats
    local points = mkStat("IntValue", "Points", 0)
    local level = mkStat("IntValue", "Level", 0)

    -- load data in

    points.Parent = plr
    level.Parent = plr 
end)

This will create a new IntValue per player which then we can load / save data from.

Example:-

local statsDS = game:GetService("DataStoreService"):GetDataStore("StoreName")
local prefix = "usr_"

-- local functions
local function mkStat(statType, name, val)
    local tmp = Instance.new(statType)
    tmp.Name = name

    if val ~= nil then
        tmp.Value = val
    end

    return tmp
end

game.Players.PlayerAdded:Connect(function(plr)
    local points = mkStat("IntValue", "Points", 0)
    local level = mkStat("IntValue", "Level", 0)

    -- load data in
    local data = ST:GetAsync(prefix .. tostring(plr.UserId))
    if data then
        print("Data loaded for " .. prefix .. tostring(plr.UserId))
        points.Value = data[1]
        level.Value = data[2]
    end

    points.Parent = plr
    level.Parent = plr 
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local data = {
        plr:WaitForChild("Points").Value,
        plr:WaitForChild("Level").Value
    }
    -- pass a table to save
    ST:SetAsync(prefix .. tostring(plr.UserId), data)
end)

This is just a basic example and does not handle any data store errors so loss of player data is possible. I would also recommend against using a array of data for loading / saving as if one value is not present then others will be out of order.

Instead use a dictionary i.e.

local data = {}
for name, ins in pairs(plr:GetChildren()) do
    data[name] = ins.Value
end

But this will mean that you would need to change how you load data in.

I hope this helps

Answer this question