How To Overwrite Values In DataStore?

So I want to overwrite the player’s data, which is in this table, but the data only saves in one place under a game. I want to overwrite the player’s data when they join place 2 and so on. Whenever I join the second place, it has my data from the first place. But when I leave the second place, the data doesn’t save, it only saves if I leave the first place.

DataStore Script in ServerScriptService, this scipt is used in both places, exactly the same.

--Data Store
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")

local players = game:GetService("Players")

local function save(player)
	local userID = player.UserId
	local key = "Player_"..userID
--[[
	local backpack = player:WaitForChild("Backpack")
	local tool = backpack:WaitForChild("P9")
	local P9Ammo = tool:WaitForChild("Ammo")
	local P9MaxAmmo = tool:WaitForChild("MaxAmmo")
--]]

	local playerData = {
		game.ReplicatedStorage.PlayerData.GotFlashDrive.Value;
		game.ReplicatedStorage.PlayerData.GotWeaponParts.Value;
		game.ReplicatedStorage.PlayerData.GotArmor.Value;
		game.ReplicatedStorage.PlayerData.P9Ammo.Value;
		game.ReplicatedStorage.PlayerData.P9MaxAmmo.Value;
		game.ReplicatedStorage.PlayerData.MaleFlag.Value;
		game.ReplicatedStorage.PlayerData.FemaleFlag.Value
	}

	local success, err = pcall(function()
		PlayerDataStore:SetAsync(key, playerData)
	end)

	if success then
		print("The data Have Been SAVED WOO")
		print("This is data flashdrive witherws: "..game.ReplicatedStorage.PlayerData.GotFlashDrive.Value)
	else
		print("Aw man, the data didn't save.")
		warn(err)
	end

end

local function onShutDown()
	task.wait(6)
end

local function setUp(player)
	local userID = player.UserId
	local key = "Player_"..userID
--[[
	local backpack = player:WaitForChild("Backpack")
	local tool = backpack:WaitForChild("P9")
--]]
	local flashDrive = game.ReplicatedStorage.PlayerData.GotFlashDrive
	local weaponParts = game.ReplicatedStorage.PlayerData.GotWeaponParts
	local gotArmor = game.ReplicatedStorage.PlayerData.GotArmor
	local P9Ammo = game.ReplicatedStorage.PlayerData.P9Ammo
	local P9MaxAmmo = game.ReplicatedStorage.PlayerData.P9MaxAmmo
	local male = game.ReplicatedStorage.PlayerData.MaleFlag
	local female = game.ReplicatedStorage.PlayerData.FemaleFlag

	local data
	local success, err = pcall(function()
		data = PlayerDataStore:GetAsync(key)
	end)

	if success and data then
		flashDrive.Value = data[1]
		weaponParts.Value = data[2]
		gotArmor.Value = data[3]
		P9Ammo.Value = data[4]
		P9MaxAmmo.Value = data[5]
		male.Value = data[6]
		female.Value = data[7]
	else
		print("Uh oh... Player didnt have any data!1!11!")
	end
end

players.PlayerAdded:Connect(setUp)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			save(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		save(player)
	end)

	if success then
		print("Omg you're so smart, data saved.")
	else
		print("Data didn't save")
	end
end)
4 Likes

You want to Overwrite or Update the Datastore of the player?

2 Likes

What’s the difference between the two?

2 Likes

When I think on Updating, I think that you want to read the previous saved data, include new data of the current session and save it.

When I think on Overwrite I kinda imagine you want to ignore previous data, and that each place has its own stuff, like, you only care whats the last place a player visited.

Targeting your issue, when using that script on both places, when you leave place 2, you mean nothing is saved? no warns o reasons?

2 Likes

I see, in that case I think I want to update the previous saved data. Because in my game, once you leave place 1, you can’t go back to place 1 and you can only progress forward to place 2 and other places.

When I leave the place 2, it says that the data is saved, but when I join back. It gives me the same values from place 1.

2 Likes

use variables

local RS = game:GetService("ReplicatedStorage")
local PlayerData = RS.PlayerData -- Use variables
local Data = {} -- Instead of a wall of values, you could loop
for _, DataValue in PlayerData:GetChildren() do
  table.insert(Data, DataValue.Value)
end
...
PlayerDataStore:SetAsync(_, Data)
2 Likes

To join place2, you visited first place1? or you directly go to place2?

2 Likes

First you visit place 1, then when you complete it. You go to place 2 and can’t go back to place 1.
Also I have a main menu, which is the game itself so you technically join that first then place 1.

2 Likes

Oh, is using variables faster and better in general scripting?

4 Likes

If you are visiting place1 you are overwriting the data of the datastore, thats why when you join place2 you have the data of place1 again then, isnt?

2 Likes

well why would you save previous data if you only want to go foward

3 Likes

instead of typing (or pasting) game.ReplicatedStorage.PlayerData 7 times
variables can help a lot

2 Likes

Oh I think that is what’s happening. I’m not really well versed in data stores, so I don’t know how to update data.

2 Likes

That actually makes more sense to me than what I did, thank you for helping me understand this!

2 Likes

Here’s the thing.

When you visit place 1 by first time, you receive somekind of “starter pack” on the SetUp function. Then you leave and join place2 and you are not READING the previous data in the players Datastore, you are directly giving again the same “starter pack”… player leaves game and save the current data of place2…
Now, when you join game again on place1, you are not READING again the datastore, you are overwriting that with the StarterPack on place1, player leaves, data gets saved, and player joins place2, and again and again, like a loop of overwriting.

You are missing the GetAsync or UpdateAsync, to read the data of the player and Update

3 Likes

Hmm, that sounds a lot like something like this:

→ Player joins Place1
→ Player data loads
→ Player quickly switches to Place2
→ Data loads in Place2 before Place1 can save the new data
→ Place1 finally saves the new data, but old data is still loaded in another live game
→ Player is now stuck with old data in Place2, and can’t leave without the new data being overwritten by the old data from when they started in Place1

2 Likes

That explanation made me understand this situation more, but I used the GetAsync function in my setUp funtion. Unless it’s different from what you were trying to say?

(The code for reference)

local function setUp(player)
	local userID = player.UserId
	local key = "Player_"..userID
--[[
	local backpack = player:WaitForChild("Backpack")
	local tool = backpack:WaitForChild("P9")
--]]
	local flashDrive = game.ReplicatedStorage.PlayerData.GotFlashDrive
	local weaponParts = game.ReplicatedStorage.PlayerData.GotWeaponParts
	local gotArmor = game.ReplicatedStorage.PlayerData.GotArmor
	local P9Ammo = game.ReplicatedStorage.PlayerData.P9Ammo
	local P9MaxAmmo = game.ReplicatedStorage.PlayerData.P9MaxAmmo
	local male = game.ReplicatedStorage.PlayerData.MaleFlag
	local female = game.ReplicatedStorage.PlayerData.FemaleFlag

	local data
	local success, err = pcall(function()
		data = PlayerDataStore:GetAsync(key)
	end)

	if success and data then
		flashDrive.Value = data[1]
		weaponParts.Value = data[2]
		gotArmor.Value = data[3]
		P9Ammo.Value = data[4]
		P9MaxAmmo.Value = data[5]
		male.Value = data[6]
		female.Value = data[7]
	else
		print("Uh oh... Player didnt have any data!1!11!")
	end
end
players.PlayerAdded:Connect(setUp)
2 Likes

Yeah, that sounds about right. Basically the player is stuck with place 1’s data whenever the player leaves place 1 or 2.

2 Likes

My bad, sorry for not reading your script carefully and just guessing.
First, when is this folder created? game.ReplicatedStorage.PlayerData?

What it contains? cause you are saving those values over and over on each place, are really changing?

Im gonna guess its a game for 1 player?

2 Likes

It’s alright, I get it cause I do that too if I help others with coding lol.
This folder is under replicated storage and here is a screenshot below and it contains all of those values. It’s going to stay there in replicated storage and it wasn’t added by a script or anything like that.
image
Also your guess is correct. I’m starting with single player games and working my way up to making multiplayer games.

3 Likes