I need saving system for my tower shop

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)
2 Likes

Make a module that loads a tower and save it to a datastore for each plr and when they join the module calls a tower they own and gives it to them.

Do you mean a module script or what?

Yes make a module that you can require to get towers to give to a plr

Im a beginner scripter but I will try it.

1 Like

Is there another way to do it?

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! :wink:

Im confused can you tell this step by step please

Are you saying you want the saving system which means it’ll still save and load if you join a different or rejoin a server?

I want a saving system for my tower shop if a player bought a tower it’ll save the tower and player cant bought it again

Ok so the module would be like this

Blockquote

module = {}

    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

Ok thank you so much for scripting that for me

1 Like

And where i need to put

for i,v in pairs(savedTowers) do
    require(moduleLocation):LoadTower(v,plr)
end

And

require(moduleLocation):LoadTower(towerneedetoload,plr)

By the way i know i need to change moduleLocation

And its not working with my script

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!

I changed it little bit and it worked! Thanks so much!

1 Like