hello! Im making a weapon buying and unlocking system where players can unlock weapons and then buy them. when the player joins i want it to clone the templates and when the player unlocks a new weapon it unlocks the weapon on the template. However i kinda of stuck on how i should clone the templates.
keep in mind my module script includes data stores so i cannot be used in the client…
– module
local weaponModuleStats = {}
local DataStoreService = game:GetService("DataStoreService")
local weaponDamageData = DataStoreService:GetDataStore("weaponStats")
weaponModuleStats.weaponStats = {
["punch"] = {damage = 5, price = 0, description = "you punch people i guess. not much to be said", image = "rbxassetid://17736469807", unlockable = false, unlocked = true},
["bat"] = {damage = 9, price = 95, description = "an average baseball bat." , image = "rbxassetid://9831327336", unlockable = false, unlocked = true},
}
function weaponModuleStats:saveWeaponStats(player, statsTable)
local success, errorMessage = pcall(function()
weaponDamageData:SetAsync(player.UserId .. "-weaponStats", statsTable)
end)
if not success then
warn("Failed to save weapon damage: " .. errorMessage)
end
end
function weaponModuleStats:loadWeaponStats(player)
local success, weaponStatsTable = pcall(function()
return weaponDamageData:GetAsync(player.UserId .. "-weaponStats")
end)
if success and weaponStatsTable then
return weaponStatsTable
else
if not success then
warn("Failed to load weapon damage: " .. weaponStatsTable)
end
return weaponModuleStats.weaponStats
end
end
function weaponModuleStats:getWeaponAttribute(player, weaponName, attribute)
local playerWeaponStatTable = self:loadWeaponStats(player)
if playerWeaponStatTable[weaponName] then
return playerWeaponStatTable[weaponName][attribute] or weaponModuleStats.weaponStats[weaponName][attribute]
else
return weaponModuleStats.weaponStats[weaponName][attribute]
end
end
function weaponModuleStats:updateWeaponAttribute(player, weaponName, attribute, amount)
local playerWeaponStatTable = self:loadWeaponStats(player)
if not playerWeaponStatTable[weaponName] then
playerWeaponStatTable[weaponName] = {}
end
if not playerWeaponStatTable[weaponName][attribute] then
playerWeaponStatTable[weaponName][attribute] = weaponModuleStats.weaponStats[weaponName][attribute] or 0
end
playerWeaponStatTable[weaponName][attribute] = playerWeaponStatTable[weaponName][attribute] + amount
self:saveWeaponStats(player, playerWeaponStatTable)
end
function weaponModuleStats:getUnlockedWeapons(player)
local playerWeaponStatTable = self:loadWeaponStats(player)
local unlockedWeapons = {}
for weaponName, attributes in pairs(playerWeaponStatTable) do
if attributes.unlockable == true then
if attributes.unlocked == true then
table.insert(unlockedWeapons, weaponName)
end
end
end
return unlockedWeapons
end
function weaponModuleStats:getNotUnlockedWeapons(player)
local playerWeaponStatTable = self:loadWeaponStats(player)
local notUnlockedWeapons = {}
for weaponName, attributes in pairs(playerWeaponStatTable) do
if attributes.unlockable == true then
if attributes.unlocked == false then
table.insert(notUnlockedWeapons, weaponName)
end
end
end
return notUnlockedWeapons
end
function weaponModuleStats:unlockWeapon(player, weaponName)
local playerWeaponStatTable = self:loadWeaponStats(player)
if playerWeaponStatTable[weaponName] then
if playerWeaponStatTable[weaponName]["unlockable"] and playerWeaponStatTable[weaponName]["unlockable"] == true then
playerWeaponStatTable[weaponName].unlocked = true
self:saveWeaponStats(player, playerWeaponStatTable)
end
end
end
return weaponModuleStats
– local
local openNpcShopModule = require(replicatedStorage:WaitForChild("mechanics"):WaitForChild("Ui"):WaitForChild("openNpcShop"))
local openShopEvent = replicatedStorage:WaitForChild("mechanics"):WaitForChild("Ui"):WaitForChild("openFolder"):FindFirstChild("weaponDealerOpen")
local boughtEvent = replicatedStorage:WaitForChild("mechanics"):WaitForChild("npcs"):WaitForChild("weaponDealer"):WaitForChild("buyWeapon")
local weapons = replicatedStorage:WaitForChild("mechanics"):WaitForChild("npcs"):WaitForChild("weaponDealer"):WaitForChild("weapons")
local camera = workspace.Camera
local weaponStatsModule = require(replicatedStorage:WaitForChild("client"):WaitForChild("weaponStats"))
local unlockedItemsTable = weaponStatsModule:getUnlockedWeapons(player)
for i,v in ipairs(unlockedItemsTable) do
if weapons:FindFirstChild(v) then
local weapon = weapons:FindFirstChild(v)
local template = replicatedStorage:WaitForChild("mechanics"):WaitForChild("npcs"):WaitForChild("weaponDealer"):WaitForChild("template")
local lockedFrame = template:FindFirstChild("lockedFrame")
local info = template:FindFirstChild("info")
local image = template:FindFirstChild("imageFrame"):FindFirstChild("weaponImage")
local weaponName = info:FindFirstChild("weaponName")
local weaponDescription = info:FindFirstChild("weaponDescription")
local purchasebutton = info:FindFirstChild("buyButton")
lockedFrame.Visible = false
local price = weaponStatsModule:getWeaponAttribute(player, v, "price")
image.Image = weaponStatsModule:getWeaponAttribute(player, v, "image")
weaponName.Text = v
weaponDescription = weaponStatsModule:getWeaponAttribute(player, v, "description")
purchasebutton.cashText = "buy - $" .. tostring(price)
template.Name = v
template.Parent = scrollingFrame
purchasebutton.MouseButton1Click:Connect(function()
boughtEvent:FireServer(weapon, price)
end)
end
end