DataStore2 Save Applies for others

I am making a skin purchase system and it works. I can buy the skin and it stays on me. However, when someone joins after me, they get the skins that I own. Only if they join AFTER me. I can be a noob on scripting sometimes so it’s probably an obvious mistake, would be glad if anyone helped.


local DataStore2 = require(1936396537)

local DefaultValue = 0
local skinsTable = {}

game.Players.PlayerAdded:Connect(function(plr)

local winsDataStore = DataStore2("Wins",plr)
local fragmentsDataStore = DataStore2("SOUL Fragments",plr)
local skinsDataStore = DataStore2("Skins", plr)

local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local purchases = Instance.new("Folder",plr)
purchases.Name = "purchases"

local Wins = Instance.new("IntValue",leaderstats)
Wins.Name = "Wins"
local Fragments = Instance.new("IntValue",leaderstats)
Fragments.Name = "SOUL Fragments"

local function winsUpdate(updatedValue)
	Wins.Value = winsDataStore:Get(updatedValue)
end
local function fragmentsUpdate(updatedValue)
	Fragments.Value = fragmentsDataStore:Get(updatedValue)
end
local function skinUpdate(updatedValue)
	skinsTable = skinsDataStore:Get(updatedValue)
	purchases:ClearAllChildren()
	for _, skin in pairs(skinsTable) do
		local skinValue = Instance.new("BoolValue")
		skinValue.Parent = purchases
		skinValue.Name = skin
	end
end

winsUpdate(DefaultValue)
fragmentsUpdate(DefaultValue)
skinUpdate(skinsTable)

winsDataStore:OnUpdate(winsUpdate)
fragmentsDataStore:OnUpdate(fragmentsUpdate)
skinsDataStore:OnUpdate(skinUpdate)

end)

local Purchases = script.Parent.Purchases

Purchases.ChildAdded:Connect(function(process)
local Product = process.Value
local Price = process.Price.Value
local Buyer = process.Buyer.Value
local Player = game.Players:GetPlayerFromCharacter(Buyer)
local DataStore = DataStore2(“Skins”, Player)
local fragmentsDataStore = DataStore2(“SOUL Fragments”, Player)
local Skins = Player.purchases
local Skin = Instance.new(“BoolValue”)
Skin.Name = Product
Skin.Parent = Skins
local SkinTable = {}
for _, v in ipairs(Skins:GetChildren()) do
table.insert(SkinTable, v.Name)
end
fragmentsDataStore:Increment(-Price,DefaultValue)
DataStore:Set(SkinTable)
end)

when you insert a variable:

local Coins = {}
local Experience = {}

server-sided

this local Coins = {} is a global value

to fix and make it ‘‘Per player’’

you will need to remove and insert all variables inside

game.Players.PlayerAdded:Connect(function(plr)

-- Inside of this, since, For each player that has been added, it will create a unique
individual Table or value

end)

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