Hello, so I am learning how to save player data and I am making a Clothes Shop. Now I have to save the clothes that players bought. I can save those but when I try to save the cash, it is unable to be saved. Can someone help me with this code?
local ds = game:GetService("DataStoreService"):GetDataStore("PlayerDataStrings09")
local ds2 = game:GetService("DataStoreService"):GetDataStore("PlayerCash07")
game.Players.PlayerAdded:connect(function(player)
local key = "user:"..player.userId
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local cash = Instance.new("IntValue",folder)
cash.Name = "Cash"
cash.Value = 1000
local folder2 = Instance.new("Folder",player)
folder2.Name = "Clothes"
local currentoutfit = Instance.new("StringValue",folder2)
currentoutfit.Name = "CurrentOutfit"
local shirt = Instance.new("StringValue",currentoutfit)
shirt.Name = "Shirt"
local pants = Instance.new("StringValue",currentoutfit)
pants.Name = "Pants"
player.CharacterAdded:connect(function(character)
if shirt.Value ~= "" then
character:WaitForChild("Shirt").ShirtTemplate = shirt.Value
character:WaitForChild("Pants").PantsTemplate = pants.Value
end
end)
local save = ds:GetAsync(key)
if save then
cash.Value = save[1]
for i,v in pairs(save) do
if game.ReplicatedStorage.Clothes:FindFirstChild(v) then
local realClothes = game.ReplicatedStorage.Clothes:FindFirstChild(v):Clone()
realClothes.Parent = folder2
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "user:"..player.userId
local cash = player.Cash
ds:SetAsync(key,cash.Value)
end)
game.ReplicatedStorage.BuyShirt.OnServerEvent:Connect(function(player,shirt,pants,cName)
local key = "user:"..player.userId
player.Character:WaitForChild("Shirt").ShirtTemplate = shirt
player.Character:WaitForChild("Pants").PantsTemplate = pants
player:WaitForChild("Clothes").CurrentOutfit.Shirt.Value = shirt
player:WaitForChild("Clothes").CurrentOutfit.Pants.Value = pants
local clothes = Instance.new("BoolValue",player.Clothes)
clothes.Name = cName
local newShirt =Instance.new("StringValue",clothes)
newShirt.Name = "Shirt"
newShirt.Value = shirt
local newPants =Instance.new("StringValue",clothes)
newPants.Name = "Pants"
newPants.Value = pants
local clothes = {}
for i,v in pairs(player.Clothes:GetChildren()) do
if v.Name ~= "CurrentOutfit" then
table.insert(clothes,v.Name)
ds:SetAsync(key,clothes)
end
end
end)

2 comments
you need to save the money
That's what I did there