I have a script that whenever a player purchases a sword, a new StringValue gets put into a folder called “OwnedSwords”. When the StringValue gets added, I want to adjust the script so that when the player leaves the game, then all the children get saved and then get reput into the “OwnedSwords” when the player rejoins. This is the script that adds the StringValue when the player purchases a sword;
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swordPurchasedEvent = ReplicatedStorage.Events:WaitForChild("SwordPurchasedEvent")
local swordPrices = {
["Spartan Sword"] = 500,
["Steampunk Sword"] = 1000,
["Skelly Slayer"] = 1500,
["Signifier Sword"] = 2000,
["Darkheart"] = 2750,
["Overseer Wrath"] = 5000,
["Serpent Sword"] = 6000,
["Light Blade"] = 7500,
["Plated Sword"] = 9250,
["Uppercut Sword"] = 11000,
["Evil Knight"] = 13450,
["Arabian Knight"] = 16000,
["Spec Elipson"] = 19000,
["Interstellar Sword"] = 23500,
["RGB Sword"] = 31000,
["Violet Sword"] = 40000,
["Gilded Sword"] = 52500,
["Omi Odachi Sword"] = 65000,
["Orc Blade"] = 80000,
["Sorcus Sword"] = 97500,
["Sword Of Darkness"] = 111000,
["Amethyst Rock Sword"] = 125000,
["Blood Sword"] = 200000,
["Molten Scythe"] = 250000,
}
swordPurchasedEvent.OnServerEvent:Connect(function(player, sword)
print("Received sword:", sword)
local price = swordPrices[tostring(sword)]
print("Price:", price)
if price then
if player.leaderstats.Gems.Value >= price then
print("Deducting gems...")
player.leaderstats.Gems.Value = player.leaderstats.Gems.Value - price
print("Gems deducted successfully.")
local stringValue = Instance.new('StringValue', player)
stringValue.Parent = player.OwnedSwords
stringValue.Name = tostring(sword)
else
print("Player does not have enough gems to purchase.")
end
else
print("Sword price not found.")
end
end)
And the script that creates the folder when the player joins is:
game.Players.PlayerAdded:Connect(function(player)
local swordFolder = Instance.new('Folder', player)
swordFolder.Name = "OwnedSwords"
end)
Any help or guidance would be appreciated, thanks alot.