Here is the tower shop code I need a saving mechanic for this
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buyTowerEvent = ReplicatedStorage:WaitForChild("BuyTower")
local Towers = {
{
Name = "Minigunner",
Money = 3500,
IsEquipped = false,
IsBought = false
}
}
buyTowerEvent.OnServerEvent:Connect(function(Player, TowerName)
print(Player, TowerName)
for _, Tower in pairs(Towers) do
if Tower.Name == TowerName then
local leaderboard = Player:FindFirstChild("leaderboard")
if not leaderboard then return end
if leaderboard.Money.Value >= Tower.Money and not Tower.IsBought and not Tower.IsEquipped then
leaderboard.Money.Value -= Tower.Money
Tower.IsBought = true
end
break
end
end
end)
You could make a bindable function and make it get the tower and return the object while cloning the tower needed for loading. And you can take the tower and load it on the player. But that’s your decision! BindableFunction | Documentation - Roblox Creator Hub if you need info about them!
function module:LoadTower(name,plr)
local repos = thelocationofthetowers
if repos:FindFirstChild(name) then
local clone = repos:FindFirstChild(name):Clone()
local plrOwnedTowers = wherethetowersgotobeloaded
clone.Parent = plrOwnedTowers
local
end
end
return module
after this you can store and save a plr when they join and leave
and require the module by doing require(moduleLocation):LoadTower(towerneedetoload,plr)
and you can do something like this aswell.
for i,v in pairs(savedTowers) do
require(moduleLocation):LoadTower(v,plr)
end
Okay we first we need a datastore if you dont already have one so we’ll do something like this.
local TowerData = game:GetService(“DataStoreService”):GetDataStore(“NameOfYourTowerDataStore”)
local LoadTowerModule = require(moduleLocation) – the Module location should be in replicated storage and you should call it like game.ReplicatedStorage:FindFirstChild(moduleName)
game.Players.PlayerAdded:Connect(function(Player)
if TowerData:GetAsync(Player.Name…“-Towers”) ~= nil then
local Data = TowerData:GetAsync(Player.Name…“-Towers”)
if Data then
local Tower1 = Data[1] – if you had more than one tower you would do Data[2]
LoadTowerModule:LoadTower(Tower1,Player)
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Num = 0
local Towers = {}
for i,v in pairs(thePlaceYouStoreYourTowers:GetChildren()) do
Num = Num + 1
table.insert(Towers,Num,v.Name)
end
TowerData:SetAsync(Player.Name…“-Towers”,Towers)
end)
– Hopefully you understand what i was trying to show you in this code. Also the module code should be up top and you should set your own variables good luck!