Advertisement
Timon-Gun

скрипты ттд 3 части кому надо

Oct 7th, 2023
3,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. -- 1 скрипт в startergui - shopgui --
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))
  4.  
  5. local getDataFunc = ReplicatedStorage:WaitForChild("GetData")
  6. local interactItemFunc = ReplicatedStorage:WaitForChild("InteractItem")
  7.  
  8. local gui3 = script.Parent.InventoryContainer
  9. local inventoryframe = gui3.ItemsFrame
  10. local exit3 = gui3.Exit
  11. local limit2 = gui3.Limit
  12.  
  13. local gui = script.Parent
  14. local exit = gui.Container.Exit
  15. local coins = gui.Container.Coins
  16. local limit = gui.Limit
  17. local itemsFrame = gui.Container.ItemsFrame
  18.  
  19.  
  20. local playerData = {}
  21.  
  22. local function getItemStatus(itemName)
  23. if table.find(playerData.SelectedTowers, itemName) then
  24. return "Equipped"
  25. elseif table.find(playerData.OwnedTowers, itemName) then
  26. return "Owned"
  27. else
  28. return "For Sale"
  29. end
  30. end
  31.  
  32. local function interactItem(itemName)
  33. local data = interactItemFunc:InvokeServer(itemName)
  34. if data then
  35. playerData = data
  36. updateItems()
  37. end
  38. end
  39.  
  40. function updateItems()
  41. coins.Text = "💵 " .. playerData.Coins
  42.  
  43. limit.Text = #playerData.SelectedTowers .. "/5"
  44. limit2.Text = "Equipped Units " .. #playerData.SelectedTowers .. "/5"
  45.  
  46.  
  47. for i, tower in pairs(towers) do
  48. -- Find any old buttons
  49. local oldButton = itemsFrame:FindFirstChild(tower.Name)
  50. if oldButton then
  51. oldButton:Destroy()
  52. end
  53.  
  54. local oldButton2 = inventoryframe:FindFirstChild(tower.Name)
  55. if oldButton2 then
  56. oldButton2:Destroy()
  57. end
  58.  
  59. local oldButton3 = gui["Units Gread"]:FindFirstChild(tower.Name)
  60. if oldButton3 then
  61. oldButton3:Destroy()
  62. end
  63.  
  64. -- Creating new button
  65. local newButton = itemsFrame.TemplateButton:Clone()
  66. newButton.Name = tower.Name
  67. newButton.TowerName.Text = tower.Name
  68. newButton.Image = tower.ImageAsset
  69. newButton.Visible = true
  70. newButton.LayoutOrder = tower.Price
  71. newButton.Parent = itemsFrame
  72.  
  73. local newButton2 = inventoryframe.TemplateButton:Clone()
  74. newButton2.Name = tower.Name
  75. newButton2.TowerName.Text = tower.Name
  76. newButton2.Image = tower.ImageAsset
  77. newButton2.Visible = false
  78. newButton2.LayoutOrder = tower.Price
  79. newButton2.Parent = inventoryframe
  80.  
  81. local newButton3 = gui["Units Gread"].Template:Clone()
  82. newButton3.Name = tower.Name
  83. newButton3.TowerName.Text = tower.Name
  84. newButton3.Image = tower.ImageAsset
  85. newButton3.Visible = false
  86. newButton3.Parent = gui["Units Gread"]
  87.  
  88. local status = getItemStatus(tower.Name)
  89. if status == "For Sale" then
  90. newButton.Status.Text = "💵" .. tower.Price
  91. elseif status == "Equipped" then
  92. newButton.Status.Text = "✅ Equipped"
  93. newButton3.Visible = true
  94. newButton.Status.TextColor3 = Color3.new(1, 1, 1)
  95. newButton.BackgroundColor3 = Color3.new(1, 1, 1)
  96. else
  97. newButton.Status.Text = "Equip"
  98. end
  99.  
  100. newButton.Activated:Connect(function()
  101. interactItem(tower.Name)
  102. end)
  103. local status2 = getItemStatus(tower.Name)
  104. if status2 == "For Sale" then
  105. elseif status2 == "Equipped" then
  106. newButton2.Visible = true
  107. newButton2.Status.Text = "✅ Equipped"
  108. newButton2.Status.TextColor3 = Color3.new(1, 1, 1)
  109. newButton2.BackgroundColor3 = Color3.new(1, 1, 1)
  110. else
  111. newButton2.Status.Text = "Equip"
  112. newButton2.Visible = true
  113. end
  114.  
  115. newButton2.Activated:Connect(function()
  116. interactItem(tower.Name)
  117. newButton3.Visible = false or newButton3.Visible == true
  118. end)
  119. newButton3.Activated:Connect(function()
  120. interactItem(tower.Name)
  121. newButton3.Visible = false or newButton3.Visible == true
  122.  
  123. end)
  124. end
  125. end
  126.  
  127. local function toggleShop()
  128. gui.Container.Visible = not gui.Container.Visible
  129. if gui.Container.Visible then
  130. playerData = getDataFunc:InvokeServer()
  131. updateItems()
  132.  
  133.  
  134. end
  135. end
  136. local function toggleShop2()
  137. gui3.Visible = false
  138. if gui3.Visible then
  139. playerData = getDataFunc:InvokeServer()
  140. updateItems()
  141. end
  142. end
  143.  
  144. local function setupShop()
  145. local prompt = Instance.new("ProximityPrompt")
  146. prompt.RequiresLineOfSight = false
  147. prompt.ActionText = "Shop"
  148. prompt.Parent = workspace:WaitForChild("ShopPart")
  149. prompt.HoldDuration = 1
  150. prompt.Triggered:Connect(toggleShop)
  151. exit.Activated:Connect(toggleShop)
  152. local towershop = gui.TowerShopButton.MouseButton1Click:Connect(function()
  153. gui.Container.Visible = not gui.Container.Visible
  154.  
  155. playerData = getDataFunc:InvokeServer()
  156. updateItems()
  157. end)
  158. end
  159.  
  160. local function setupinventory()
  161. local towerinventory = gui.TowerInventoryButton.MouseButton1Click:Connect(function()
  162. if gui3.Visible == false then
  163. gui3.Visible = true
  164. else
  165.  
  166. end
  167.  
  168. playerData = getDataFunc:InvokeServer()
  169. updateItems()
  170. exit3.Activated:Connect(toggleShop2)
  171. end)
  172. end
  173. setupinventory()
  174. setupShop()
  175.  
  176. -- 2 скрипт в server script service --
  177. local DataStoreService = game:GetService("DataStoreService")
  178. local Players = game:GetService("Players")
  179. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  180. local RunService = game:GetService("RunService")
  181.  
  182. local database = DataStoreService:GetDataStore("database1")
  183. local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))
  184.  
  185. local MAX_SELECTED_TOWERS = 5
  186.  
  187. local data = {}
  188.  
  189. -- Load the players data
  190. local function LoadData(player)
  191. local success = nil
  192. local playerData = nil
  193. local attempt = 1
  194.  
  195. repeat
  196. success, playerData = pcall(function()
  197. return database:GetAsync(player.UserId)
  198. end)
  199.  
  200. attempt += 1
  201. if not success then
  202. warn(playerData)
  203. task.wait()
  204. end
  205.  
  206. until success or attempt == 3
  207.  
  208. if success then
  209. print("Connection success")
  210. if not playerData then
  211. print("New player, giving default data")
  212. playerData = {
  213. ["Coins"] = 20,
  214. ["SelectedTowers"] = {"CameraMan"},
  215. ["OwnedTowers"] = {"CameraMan", "Speakerman"}
  216. }
  217. end
  218. data[player.UserId] = playerData
  219. else
  220. warn("Unable to get data for player", player.UserId)
  221. player:Kick("There was a problem getting your data")
  222. end
  223. end
  224. Players.PlayerAdded:Connect(LoadData)
  225.  
  226. -- Save the players data
  227. local function SaveData(player)
  228. if data[player.UserId] then
  229. local success = nil
  230. local playerData = nil
  231. local attempt = 1
  232.  
  233. repeat
  234. success, playerData = pcall(function()
  235. return database:UpdateAsync(player.UserId, function()
  236. return data[player.UserId]
  237. end)
  238. end)
  239.  
  240. attempt += 1
  241. if not success then
  242. warn(playerData)
  243. task.wait()
  244. end
  245.  
  246. until success or attempt == 3
  247.  
  248. if success then
  249. print("Data saved successfully")
  250. else
  251. warn("Unable to save data for", player.UserId)
  252. end
  253. else
  254. warn("No session data for", player.UserId)
  255. end
  256.  
  257. end
  258. Players.PlayerRemoving:Connect(function(player)
  259. SaveData(player)
  260. data[player.UserId] = nil
  261. end)
  262.  
  263. game:BindToClose(function()
  264. if not RunService:IsStudio() then
  265. for index, player in pairs(Players:GetPlayers()) do
  266. task.spawn(function()
  267. SaveData(player)
  268. end)
  269. end
  270. else
  271. print("Shutting down inside studio")
  272. end
  273. end)
  274.  
  275. local function getItemStatus(player, itemName)
  276. local playerData = data[player.UserId]
  277. if table.find(playerData.SelectedTowers, itemName) then
  278. return "Equipped"
  279. elseif table.find(playerData.OwnedTowers, itemName) then
  280. return "Owned"
  281. else
  282. return "For Sale"
  283. end
  284. end
  285.  
  286. ReplicatedStorage.InteractItem.OnServerInvoke = function(player, itemName)
  287. local shopItem = towers[itemName]
  288. local playerData = data[player.UserId]
  289. if shopItem and playerData then
  290. local status = getItemStatus(player, itemName)
  291.  
  292. if status == "For Sale" and shopItem.Price <= playerData.Coins then
  293. -- purchase
  294. playerData.Coins -= shopItem.Price
  295. table.insert(playerData.OwnedTowers, shopItem.Name)
  296.  
  297. elseif status == "Owned" then
  298. -- equip the tower
  299. table.insert(playerData.SelectedTowers, shopItem.Name)
  300. if #playerData.SelectedTowers > MAX_SELECTED_TOWERS then
  301. table.remove(playerData.SelectedTowers, 1)
  302. end
  303.  
  304. elseif status == "Equipped" then
  305. -- unselect the tower (if more than 1 selected)
  306. if #playerData.SelectedTowers > 1 then
  307. local towerToRemove = table.find(playerData.SelectedTowers, itemName)
  308. table.remove(playerData.SelectedTowers, towerToRemove)
  309. end
  310. end
  311.  
  312. return playerData
  313. else
  314. warn("Tower/plaer data does not exist")
  315. end
  316.  
  317. return false
  318. end
  319.  
  320. ReplicatedStorage.GetData.OnServerInvoke = function(player)
  321. return data[player.UserId]
  322. end
  323. -- ну и 3 скрипт в replicated storage --
  324. local TowerShop = {
  325.  
  326. ["CameraMan"] = {
  327. ["Name"] = "CameraMan",
  328. ["ImageAsset"] = "rbxassetid://14922076623 ",
  329. ["Price"] = 0,
  330.  
  331. },
  332. ["Speakerman"] = {
  333. ["Name"] = "Speakerman",
  334. ["ImageAsset"] = "rbxassetid://14950583537",
  335. ["Price"] = 0,
  336. },
  337. ["TvMan"] = {
  338. ["Name"] = "TvMan",
  339. ["ImageAsset"] = "rbxassetid://14846429584",
  340. ["Price"] = 1500,
  341. },
  342.  
  343. }
  344.  
  345.  
  346. return TowerShop
  347.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement