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