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

Help With DataStore?

I'm currently using DataStore to store two things: Inventory and wins/coin counting.

When it comes to the wins and the coins, it saves fine, but when it comes to saving a few other variables I'm using for an Inventory, it doesn't work so well.

It works in game, but once I leave the data resets. Help?

Joining game/loading data

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

game.Players.PlayerAdded:connect(function(player)
	
	local leaderstats = Instance.new("Model", player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Coins"
	
	local wins = Instance.new("IntValue", leaderstats)
	wins.Name = "Wins"
	
	local InvoSlot1 = Instance.new("StringValue", player)
	InvoSlot1.Name = "Slot1"
	InvoSlot1.Value = "Default"
	
	local InvoSlot2 = Instance.new("StringValue", player)
	InvoSlot2.Name = "Slot2"
	InvoSlot2.Value = "Empty"
	
	local InvoSlot3 = Instance.new("StringValue", player)
	InvoSlot3.Name = "Slot3"
	InvoSlot3.Value = "Empty"
	
	local InvoSlot4 = Instance.new("StringValue", player)
	InvoSlot4.Name = "Slot4"
	InvoSlot4.Value = "Empty"
	
	local Equiped = Instance.new("StringValue", player)
	Equiped.Name = "Equiped"
	Equiped.Value = "Default"
	
	
	local key = "player-"..player.userId
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		cash.Value = savedValues[1]
		wins.Value = savedValues[2]
	else
		local valuesToSave = {cash.Value, wins.Value, InvoSlot1.Value, InvoSlot2.Value, InvoSlot3.Value, InvoSlot4.Value, Equiped.Value, }
		DataStore:SetAsync(key, valuesToSave)
	end
	
	
	
end)

Leaving game/saving data

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

game.Players.PlayerRemoving:connect(function(player)
	
	local key = "player-"..player.userId
	
	local valuesToSave = {player.leaderstats.Coins.Value, player.leaderstats.Wins.Value, player.Slot1.Value, player.Slot2.Value, player.Slot3.Value, player.Slot4.Value, player.Equiped.Value}
	DataStore:SetAsync(key, valuesToSave)
	
	
end)

It has trouble saving saving everything except for "coins" and "wins"

1 comment

0

I am having problems with saving items too. This could help me out aswell.

1 answer

✓ Accepted answer
    if savedValues then
        cash.Value = savedValues[1]
        wins.Value = savedValues[2]

Try adding to other values perhaps? As it is saving the values but you need to set the local values so the player can see what they have. The player does not see what their inventory is because you're not setting the values to it for example with cash and wins you are setting that to their savedValues, you need to do the same with the inventory:

    if savedValues then
        cash.Value = savedValues[1]
        wins.Value = savedValues[2]
	InvoSlot1.Value = savedValues[3]
	InvoSlot2.Value = savedValues[4]
	InvoSlot3.Value = savedValues[5]
	InvoSlot4.Value = savedValues[6]
	Equiped.Value = savedValues[7]

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