Issue with datastore

hello

I have issue with datastore

i want to save 2 values
image

but when i want save coins value
the multiplier value save as coins value

image
image

but coins value like multiplier value
image

thank you for read

Could you show your entire script? We can’t just fix it from a small snippet.

1 Like

the script

local datastoreser = game:GetService("DataStoreService")
local playerdatastore = datastoreser:GetDataStore("playerdatastore")
game.Players.PlayerAdded:Connect(function(player)
	
	local lod = Instance.new("Folder",player)
	lod.Name = "lod"
	
	 local coins = Instance.new("NumberValue",lod)
	coins.Name = "coins"
	local Multiplier = Instance.new("IntValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	Multiplier.Parent = lod
	
	local playerid = 'Player_'..player.UserId
	local data = playerdatastore:GetAsync(playerid)
	
	if data then
		coins.Value = data
		Multiplier.Value = data
	else
		coins.Value = 0
		Multiplier.Value = 1
		
	end
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local suc, err = pcall(function()
		local playerid = 'Player_'..player.UserId
		playerdatastore:SetAsync(playerid, player.lod.coins.Value,player.lod.Multiplier.Value)

	end)
	if not suc then
		warn("its not work);")
	end
end)
game:BindToClose(function() -- Runs whenver the server is about to shut down/stop.
	print("STOPPED!")


	for i,player in pairs(game.Players:GetPlayers()) do
		
		local playerid = 'Player_'..player.UserId
		playerdatastore:SetAsync(playerid, player.lod.coins.Value,player.lod.Multiplier.Value)

		print("Saved the idea  for "..player.Name)
	end


end)

image
You set the values wrong here. It should be data[1], and data[2].

to be like this?
image

No. The values are being set to the entire datastore. You need to store them in a table, and use data.Coins, data.Multiplier. Save the data like:

dataStore:SetAsync(playerId, {
Coins = coins.Value,
Multiplier = multi.Value
})
2 Likes

to be like this?
image

Switch out your script with this:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local playerdatastore = DataStoreService:GetDataStore("playerdatastore")

--//Services
local function SaveData(player, data)
	local success, errorMessage = pcall(function()
		playerdatastore:SetAsync(player.UserId, data)
	end)
	
	if not success then
		warn(errorMessage)
	end
end

Players.PlayerAdded:Connect(function(player)
	local data = playerdatastore:GetAsync(player.UserId) or {
		Coins = 0,
		Multiplier = 1
	}
	
	local lod = Instance.new("Folder")
	lod.Name = "lod"
	lod.Parent = player

	local coins = Instance.new("NumberValue")
	coins.Name = "coins"
	coins.Value = data.Coins
	coins.Parent = lod
	
	local Multiplier = Instance.new("IntValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Value = data.Multiplier
	Multiplier.Parent = lod
end)

Players.PlayerRemoving:Connect(function(player)
	local data = {
		Coins = player.lod.coins.Value,
		Multiplier = player.lod.Multiplier.Value
	}
	
	SaveData(player, data)
end)

game:BindToClose(function()
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			local data = {
				Coins = player.lod.coins.Value,
				Multiplier = player.lod.Multiplier.Value
			}

			SaveData(player, data)
		end)
	end
end)

You have to insert the values into a table or dictionary and then save it.