Datastore sharing between 2 players

i am new to datastores and i cant really fix this issue.

here is the entire script.

script
local DS = game:GetService("DataStoreService")
local invstore = DS:GetDataStore("Inventory")
local RS = game:GetService("RunService")

local invdata = nil

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, err = pcall(function()
		invstore:SetAsync(plr.UserId, invdata)
		
		print(invdata)
	end)
	
	if not success then
		warn(err)
	end
	
end)

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		char.Humanoid.Died:Connect(function()
			
			local success, err = pcall(function()
				invstore:SetAsync(plr.UserId, invdata)
				
				print(plr.UserId, invdata)
			end)

			if not success then
				warn(err)
			end
			
		end)
		
	end)
	
end)

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		if char then
			
			local coro1 = coroutine.wrap(function()
				getdata(plr)
			end)
			coro1()
			
			local plrfolder = char:FindFirstChild("val"):FindFirstChild("INVENTORY")
			
			plrfolder.ChildAdded:Connect(function(item)
				
				local inv = char:FindFirstChild("val"):FindFirstChild("INVENTORY"):GetChildren()
				
				for _, items in pairs(inv) do
					invdata[items.Name] = items.Value
				end
				
				item:GetPropertyChangedSignal("Value"):Connect(function()
					
					local inv = char:FindFirstChild("val"):FindFirstChild("INVENTORY"):GetChildren()
					
					for _, items in pairs(inv) do
						invdata[items.Name] = items.Value
					end
					
				end)
				
			end)
			
			plrfolder.ChildRemoved:Connect(function(item)
				
				local inv = char:FindFirstChild("val"):FindFirstChild("INVENTORY"):GetChildren()

				for _, items in pairs(inv) do
					invdata[items.Name] = items.Value
					invdata[item.Name] = 0
				end
				
			end)
			
		end
		
	end)
	
end)

function getdata(plr)
	wait(0.1)
	local success, err = pcall(function()
		invdata = invstore:GetAsync(plr.UserId)
		print("got data!")
		
		for key, value in pairs(invdata) do
			local name = key
			local amt = value
			local intvalue = Instance.new("IntValue")
			
			intvalue.Parent = plr.Character:FindFirstChild("val"):FindFirstChild("INVENTORY")
			intvalue.Name = name
			intvalue.Value = amt
		end
		
	end)
	
	if not success then
		warn(err)
	end
	
end

i am using a key so im still not sure why its sharing data between players

The issue occurs because the data in invdata is everyone’s data, not just one player.
Consider changing it so that invdata is a dictionary containing each player’s individual data, where the player is the key, to prevent the overlap of data.

Updated Code
local DS = game:GetService("DataStoreService")
local invstore = DS:GetDataStore("Inventory")
local RS = game:GetService("RunService")
local invdata = {}
game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, err = pcall(function()
		invstore:SetAsync(plr.UserId, invdata[plr])
		invdata[plr] = nil
		print(invdata)
	end)
	
	if not success then
		warn(err)
	end
	
end)
game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		char.Humanoid.Died:Connect(function()
			
			local success, err = pcall(function()
				invstore:SetAsync(plr.UserId, invdata[plr])
				
				print(plr.UserId, invdata)
			end)
			if not success then
				warn(err)
			end
			
		end)
		
	end)
	
end)
game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		if char then
			
			local coro1 = coroutine.wrap(function()
				getdata(plr)
			end)
			coro1()
			
			local plrfolder = char:FindFirstChild("val"):FindFirstChild("INVENTORY")
			
			plrfolder.ChildAdded:Connect(function(item)
				
				local inv = char:FindFirstChild("val"):FindFirstChild("INVENTORY"):GetChildren()
				
				for _, items in pairs(inv) do
					invdata[plr][items.Name] = items.Value
				end
				
				item:GetPropertyChangedSignal("Value"):Connect(function()
					
					local inv = char:FindFirstChild("val"):FindFirstChild("INVENTORY"):GetChildren()
					
					for _, items in pairs(inv) do
						invdata[plr][items.Name] = items.Value
					end
					
				end)
				
			end)
			
			plrfolder.ChildRemoved:Connect(function(item)
				
				local inv = char:FindFirstChild("val"):FindFirstChild("INVENTORY"):GetChildren()
				for _, items in pairs(inv) do
					invdata[plr][items.Name] = items.Value
					invdata[plr][item.Name] = 0
				end
				
			end)
			
		end
		
	end)
	
end)
function getdata(plr)
	wait(0.1)
	local success, err = pcall(function()
		invdata[plr] = invstore:GetAsync(plr.UserId) or {}
		print("got data!")
		
		for key, value in pairs(invdata) do
			local name = key
			local amt = value
			local intvalue = Instance.new("IntValue")
			
			intvalue.Parent = plr.Character:FindFirstChild("val"):FindFirstChild("INVENTORY")
			intvalue.Name = name
			intvalue.Value = amt
		end
		
	end)
	
	if not success then
		warn(err)
	end
	
end
2 Likes

sorry for the late reply but it worked! thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.