Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 1 скрипт в startergui - shopgui --
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))
- local getDataFunc = ReplicatedStorage:WaitForChild("GetData")
- local interactItemFunc = ReplicatedStorage:WaitForChild("InteractItem")
- local gui3 = script.Parent.InventoryContainer
- local inventoryframe = gui3.ItemsFrame
- local exit3 = gui3.Exit
- local limit2 = gui3.Limit
- local gui = script.Parent
- local exit = gui.Container.Exit
- local coins = gui.Container.Coins
- local limit = gui.Limit
- local itemsFrame = gui.Container.ItemsFrame
- local playerData = {}
- local function getItemStatus(itemName)
- if table.find(playerData.SelectedTowers, itemName) then
- return "Equipped"
- elseif table.find(playerData.OwnedTowers, itemName) then
- return "Owned"
- else
- return "For Sale"
- end
- end
- local function interactItem(itemName)
- local data = interactItemFunc:InvokeServer(itemName)
- if data then
- playerData = data
- updateItems()
- end
- end
- function updateItems()
- coins.Text = "💵 " .. playerData.Coins
- limit.Text = #playerData.SelectedTowers .. "/5"
- limit2.Text = "Equipped Units " .. #playerData.SelectedTowers .. "/5"
- for i, tower in pairs(towers) do
- -- Find any old buttons
- local oldButton = itemsFrame:FindFirstChild(tower.Name)
- if oldButton then
- oldButton:Destroy()
- end
- local oldButton2 = inventoryframe:FindFirstChild(tower.Name)
- if oldButton2 then
- oldButton2:Destroy()
- end
- local oldButton3 = gui["Units Gread"]:FindFirstChild(tower.Name)
- if oldButton3 then
- oldButton3:Destroy()
- end
- -- Creating new button
- local newButton = itemsFrame.TemplateButton:Clone()
- newButton.Name = tower.Name
- newButton.TowerName.Text = tower.Name
- newButton.Image = tower.ImageAsset
- newButton.Visible = true
- newButton.LayoutOrder = tower.Price
- newButton.Parent = itemsFrame
- local newButton2 = inventoryframe.TemplateButton:Clone()
- newButton2.Name = tower.Name
- newButton2.TowerName.Text = tower.Name
- newButton2.Image = tower.ImageAsset
- newButton2.Visible = false
- newButton2.LayoutOrder = tower.Price
- newButton2.Parent = inventoryframe
- local newButton3 = gui["Units Gread"].Template:Clone()
- newButton3.Name = tower.Name
- newButton3.TowerName.Text = tower.Name
- newButton3.Image = tower.ImageAsset
- newButton3.Visible = false
- newButton3.Parent = gui["Units Gread"]
- local status = getItemStatus(tower.Name)
- if status == "For Sale" then
- newButton.Status.Text = "💵" .. tower.Price
- elseif status == "Equipped" then
- newButton.Status.Text = "✅ Equipped"
- newButton3.Visible = true
- newButton.Status.TextColor3 = Color3.new(1, 1, 1)
- newButton.BackgroundColor3 = Color3.new(1, 1, 1)
- else
- newButton.Status.Text = "Equip"
- end
- newButton.Activated:Connect(function()
- interactItem(tower.Name)
- end)
- local status2 = getItemStatus(tower.Name)
- if status2 == "For Sale" then
- elseif status2 == "Equipped" then
- newButton2.Visible = true
- newButton2.Status.Text = "✅ Equipped"
- newButton2.Status.TextColor3 = Color3.new(1, 1, 1)
- newButton2.BackgroundColor3 = Color3.new(1, 1, 1)
- else
- newButton2.Status.Text = "Equip"
- newButton2.Visible = true
- end
- newButton2.Activated:Connect(function()
- interactItem(tower.Name)
- newButton3.Visible = false or newButton3.Visible == true
- end)
- newButton3.Activated:Connect(function()
- interactItem(tower.Name)
- newButton3.Visible = false or newButton3.Visible == true
- end)
- end
- end
- local function toggleShop()
- gui.Container.Visible = not gui.Container.Visible
- if gui.Container.Visible then
- playerData = getDataFunc:InvokeServer()
- updateItems()
- end
- end
- local function toggleShop2()
- gui3.Visible = false
- if gui3.Visible then
- playerData = getDataFunc:InvokeServer()
- updateItems()
- end
- end
- local function setupShop()
- local prompt = Instance.new("ProximityPrompt")
- prompt.RequiresLineOfSight = false
- prompt.ActionText = "Shop"
- prompt.Parent = workspace:WaitForChild("ShopPart")
- prompt.HoldDuration = 1
- prompt.Triggered:Connect(toggleShop)
- exit.Activated:Connect(toggleShop)
- local towershop = gui.TowerShopButton.MouseButton1Click:Connect(function()
- gui.Container.Visible = not gui.Container.Visible
- playerData = getDataFunc:InvokeServer()
- updateItems()
- end)
- end
- local function setupinventory()
- local towerinventory = gui.TowerInventoryButton.MouseButton1Click:Connect(function()
- if gui3.Visible == false then
- gui3.Visible = true
- else
- end
- playerData = getDataFunc:InvokeServer()
- updateItems()
- exit3.Activated:Connect(toggleShop2)
- end)
- end
- setupinventory()
- setupShop()
- -- 2 скрипт в server script service --
- local DataStoreService = game:GetService("DataStoreService")
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local RunService = game:GetService("RunService")
- local database = DataStoreService:GetDataStore("database1")
- local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))
- local MAX_SELECTED_TOWERS = 5
- local data = {}
- -- Load the players data
- local function LoadData(player)
- local success = nil
- local playerData = nil
- local attempt = 1
- repeat
- success, playerData = pcall(function()
- return database:GetAsync(player.UserId)
- end)
- attempt += 1
- if not success then
- warn(playerData)
- task.wait()
- end
- until success or attempt == 3
- if success then
- print("Connection success")
- if not playerData then
- print("New player, giving default data")
- playerData = {
- ["Coins"] = 20,
- ["SelectedTowers"] = {"CameraMan"},
- ["OwnedTowers"] = {"CameraMan", "Speakerman"}
- }
- end
- data[player.UserId] = playerData
- else
- warn("Unable to get data for player", player.UserId)
- player:Kick("There was a problem getting your data")
- end
- end
- Players.PlayerAdded:Connect(LoadData)
- -- Save the players data
- local function SaveData(player)
- if data[player.UserId] then
- local success = nil
- local playerData = nil
- local attempt = 1
- repeat
- success, playerData = pcall(function()
- return database:UpdateAsync(player.UserId, function()
- return data[player.UserId]
- end)
- end)
- attempt += 1
- if not success then
- warn(playerData)
- task.wait()
- end
- until success or attempt == 3
- if success then
- print("Data saved successfully")
- else
- warn("Unable to save data for", player.UserId)
- end
- else
- warn("No session data for", player.UserId)
- end
- end
- Players.PlayerRemoving:Connect(function(player)
- SaveData(player)
- data[player.UserId] = nil
- end)
- game:BindToClose(function()
- if not RunService:IsStudio() then
- for index, player in pairs(Players:GetPlayers()) do
- task.spawn(function()
- SaveData(player)
- end)
- end
- else
- print("Shutting down inside studio")
- end
- end)
- local function getItemStatus(player, itemName)
- local playerData = data[player.UserId]
- if table.find(playerData.SelectedTowers, itemName) then
- return "Equipped"
- elseif table.find(playerData.OwnedTowers, itemName) then
- return "Owned"
- else
- return "For Sale"
- end
- end
- ReplicatedStorage.InteractItem.OnServerInvoke = function(player, itemName)
- local shopItem = towers[itemName]
- local playerData = data[player.UserId]
- if shopItem and playerData then
- local status = getItemStatus(player, itemName)
- if status == "For Sale" and shopItem.Price <= playerData.Coins then
- -- purchase
- playerData.Coins -= shopItem.Price
- table.insert(playerData.OwnedTowers, shopItem.Name)
- elseif status == "Owned" then
- -- equip the tower
- table.insert(playerData.SelectedTowers, shopItem.Name)
- if #playerData.SelectedTowers > MAX_SELECTED_TOWERS then
- table.remove(playerData.SelectedTowers, 1)
- end
- elseif status == "Equipped" then
- -- unselect the tower (if more than 1 selected)
- if #playerData.SelectedTowers > 1 then
- local towerToRemove = table.find(playerData.SelectedTowers, itemName)
- table.remove(playerData.SelectedTowers, towerToRemove)
- end
- end
- return playerData
- else
- warn("Tower/plaer data does not exist")
- end
- return false
- end
- ReplicatedStorage.GetData.OnServerInvoke = function(player)
- return data[player.UserId]
- end
- -- ну и 3 скрипт в replicated storage --
- local TowerShop = {
- ["CameraMan"] = {
- ["Name"] = "CameraMan",
- ["ImageAsset"] = "rbxassetid://14922076623 ",
- ["Price"] = 0,
- },
- ["Speakerman"] = {
- ["Name"] = "Speakerman",
- ["ImageAsset"] = "rbxassetid://14950583537",
- ["Price"] = 0,
- },
- ["TvMan"] = {
- ["Name"] = "TvMan",
- ["ImageAsset"] = "rbxassetid://14846429584",
- ["Price"] = 1500,
- },
- }
- return TowerShop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement