I made a tower shop but there is so much errors

What is the wrong on this code?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buyTowerEvent = ReplicatedStorage:WaitForChild("BuyTower")
--local DataStore = game:GetService("DataStoreService")
--local TowerStore = DataStore:GetDataStore("PlayerTowers")
local Players = game:GetService("Players")
local leaderboard = Players:FindFirstChild("leaderboard")

local Tower = {Minigunner}
local GUIS = {BuyTower1}

BuyTower1 = {
	whattower = Minigunner
}

Minigunner = {
	moneyvalue = 3500,
	is_equipped = false,
	is_bought = false
}

function buyTower(player)
	for _,Player in pairs(Players:GetPlayers()) do
		if Player:FindFirstChild("leaderboard") then
			Player.leaderboard.Money.Value -= GUIS.whattower.moneyvalue
		end
	end
	GUIS.whattower.is_bought = true
	buyTowerEvent:FireServer()
	print("A player bought "..GUIS.whattower)
end

game.StarterGui.Shop.ShopFrame.GUIS.MouseButton1Click:Connect(buyTower())
1 Like

What are the errors that you are getting?

1 Like

GUIS is not a valid member of Frame “StarterGui.Shop.ShopFrame”
Im getting this error

1 Like

game.StarterGui.Shop.ShopFrame.GUIS.MouseButton1Click:Connect(buyTower())
It looks like “GUIS” does not exist in the ShopFrame. Are you sure that the “GUIS” is in the ShopFrame?

1 Like

No I made GUIS a table

local GUIS = {BuyTower1}
1 Like

I am not sure if you can use table in this way, but what are you exactly trying to do?

1 Like

Im trying to do a tower shop for my tower defense game

1 Like

GUIs are instances, just think of it, if you simply name the table GUIs and put stuff in it its not going to work like that. But instead, you have to create your GUI and place them in StarterGui and then it can be referenced through Localscripts by referencing them through Player’s PlayerGui.

1 Like

How can i make the Player’s PlayerGUI

You don’t need to make PlayerGui, just make your GUI in the StarterGui and keep it there, Roblox scripts will clone them to the PlayerGui automatically. To Reference the PlayerGui in localscripts, you can do this:

local player = game.Players.LocalPlayer
local PlayerGui = player.PlayerGui 

I believe you are new to GUIs, so this article will be helpful probably:

But this script is in ServerScriptService how can I do that

In most cases, you should avoid recommend changing PlayerGui from Server, as it may create replication issues. What you can do is, use remote events and have a client side Remote handler to change the PlayerGui accordingly.

I will try if I can do it I’ll make your answer as solution

I believe I had given you a link to remote events API Reference, in your earlier post where you asked something similar. I would recommend you to go through it:

local Players = game:GetService("Players") -- get players service
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- get replicatedstorage service

local buyTowerEvent = ReplicatedStorage:WaitForChild("BuyTower") -- our remote event player will fire once they decide to buy a tower

local Towers = {
	{
		Name = "Minigunner",
		Money = 3500,
		IsEquipped = false,
		IsBought = false
	}
} -- For testing purposes we gonna store towers in this table

buyTowerEvent.OnServerEvent:Connect(function(Player, TowerName) -- when player fires this event then
	for _, Tower in pairs(Towers) do -- go through the towers table
		if Tower.Name == TowerName then -- if the name of the tower in the table is matching the tower name that player provided then
			local Leaderboard = Player:FindFirstChild("leaderboard") -- try to find the leaderboard in the player object
			if not Leaderboard then return end -- if the leaderboard object does not exist then stop
			if Leaderboard.Money.Value >= Tower.Money and not Tower.IsBought and Tower.IsEquipped then -- if player has enough money and the tower that player requested is not already bought and equipped then
				Leaderboard.Money.Value -= Tower.Money -- take away the money from the player
				Tower.IsBought = true -- mark the tower as bought
			end
			break -- stop here, don't go through the rest
		end
	end
end)

I made here some changes, this will be a server sided code (script). So you get the concept.

Thanks this is very helpful thank you all.

1 Like

And is this saving the bought towers?

Nope, I did not make it save the towers, because you asked about the errors and not the saving mechanism itself.

I need a save mechanism too but ok

Try make it yourself, and then if something is not working right then you can ask for help.