Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastore values don't save?

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?

2 comments

0

Try using :SetAsync rather than :UpdateAsync, see if that does anything

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.

1 answer

✓ Accepted answer

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)

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.