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())
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?
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.
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:
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 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.