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

Datastore values don't save?

Asked by 9 years ago

Hi,

I've been messing around with DataStores, and I thought I got it to work. Basically, there are four values, the first of which saves, but the rest don't.

Here's the code that loads/saves Stores:

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

    local cash = Instance.new("IntValue",plr)
    cash.Name = "cash"
    local heat = Instance.new("IntValue",plr)
    heat.Name = "heat"
    local health = Instance.new("IntValue",plr)
    health.Name = "health"
    local fatigue = Instance.new("IntValue",plr)
    fatigue.Name = "fatigue"

    local key = "user_"..plr.userId
    cash.Value = cashStore:GetAsync(key)
    heat.Value = heatStore:GetAsync(key)
    health.Value = healthStore:GetAsync(key)
    fatigue.Value = fatigueStore:GetAsync(key)

end)



game.Players.PlayerRemoving:connect(function(plr)

    cashStore:UpdateAsync("user_"..plr.userId,function(oldValue)
        return (plr.cash.Value or oldValue)
    end)

    heatStore:UpdateAsync("user_"..plr.userId,function(oldValue)
        return (plr.heat.Value or oldValue)
    end)

    healthStore:UpdateAsync("user_"..plr.userId,function(oldValue)
        return (plr.health.Value or oldValue)
    end)

    fatigueStore:UpdateAsync("user_"..plr.userId,function(oldValue)
        return (plr.fatigue.Value or oldValue)
    end)

end)

Honestly, I think I'm just going to save the four values as a table into DataStore, but for future reference, what's the problem here?

0
Try using :SetAsync rather than :UpdateAsync, see if that does anything Muoshuu 580 — 9y
0
I need to keep the old value in mind, and the wiki says using :Set and :Get in conjunction instead of :Update is dangerous. Tortelloni 315 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Lets look at it this way. Assuming that you have a basic understanding of tables, it works that you put in a key to get a value. The same is with datastores. SetAsync is used to set the key and value, and GetAsync is used to retrieve it. A problem with both? You have the key the same for each. Lets look at it this way in terms of variables

--key   --value
userId = cash --when you look up userId, you will get cash
userId = heat --since they have the same key, userId now will give you heat
userId = health --etc...
userId = fatigue --etc...

The solution is to make four different keys for each item saved. I like how you used the players userId when saving, just incase they change their name, and so we can keep this and add more. Using ".." you can combine strings. Such as "hi ".."there" is the same as "hi there". Try adding a string onto the player Id, such as "user_"..plr.userId.."CashStore". So, you have to make 4 different keys.

Comment if you need any more help! This is what the code should look like if I am correct...

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

    local cash = Instance.new("IntValue",plr)
    cash.Name = "cash"
    local heat = Instance.new("IntValue",plr)
    heat.Name = "heat"
    local health = Instance.new("IntValue",plr)
    health.Name = "health"
    local fatigue = Instance.new("IntValue",plr)
    fatigue.Name = "fatigue"

    local cashkey = "user_"..plr.userId.."CashSaved"
    local heatkey = "user_"..plr.userId.."HeatSaved"
    local healthkey = "user_"..plr.userId.."HealthSaved"
    local fatiguekey = "user_"..plr.userId.."FatigueSaved"
    cash.Value = cashStore:GetAsync(cashkey)
    heat.Value = heatStore:GetAsync(heatkey)
    health.Value = healthStore:GetAsync(healthkey)
    fatigue.Value = fatigueStore:GetAsync(fatiguekey)

end)



game.Players.PlayerRemoving:connect(function(plr)

    cashStore:UpdateAsync("user_"..plr.userId.."CashSaved",function(oldValue)
        return (plr.cash.Value or oldValue)
    end)

    heatStore:UpdateAsync("user_"..plr.userId.."HeatSaved",function(oldValue)
        return (plr.heat.Value or oldValue)
    end)

    healthStore:UpdateAsync("user_"..plr.userId.."HealthSaved",function(oldValue)
        return (plr.health.Value or oldValue)
    end)

    fatigueStore:UpdateAsync("user_"..plr.userId.."FatigueSaved",function(oldValue)
        return (plr.fatigue.Value or oldValue)
    end)

end)

0
Thanks! Tortelloni 315 — 9y
Ad

Answer this question