Advertisement
AlewAlow

yes

Sep 13th, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local UserInputService = game:GetService("UserInputService")
  4.  
  5. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  6. local Components = require(ReplicatedStorage.Shared.Components)
  7. local Constants = require(ReplicatedStorage.Shared.Constants)
  8.  
  9. local GetItemsInserted = require(ReplicatedStorage.Shared.Utils.GetItemsInserted)
  10. local GetItemsRemoved = require(ReplicatedStorage.Shared.Utils.GetItemsRemoved)
  11.  
  12. local localPlayerInstance = Players.LocalPlayer
  13.  
  14. local function CreateToolButtons(world, state)
  15.     if not state.HasInitializedToolButtons then
  16.         state.HasInitializedToolButtons = true
  17.        
  18.         local toolButtonContainer = ReplicatedStorage.Client.Assets.ToolButtonContainer:Clone()
  19.        
  20.         world:spawn(
  21.             Components.Canvas(),
  22.             Components.Transform({
  23.                 Position = Vector2.new(Constants.VirtualSize.X / 2, Constants.VirtualSize.Y - 32),
  24.             }),
  25.             Components.Frame({
  26.                 ZIndex = 1000,
  27.                 Size = Vector2.new(16, 16),
  28.                 Instance = toolButtonContainer,
  29.             })
  30.         )
  31.        
  32.         state.ToolButtonContainer = toolButtonContainer
  33.         state.ToolButtons = {}
  34.         state.OwnedTools = {}
  35.     end
  36.    
  37.     local oldOwnedTools = state.OwnedTools
  38.     local currentOwnedTools = {}
  39.    
  40.     state.OwnedTools = currentOwnedTools
  41.    
  42.     for toolId, tool in world:query(Components.Tool) do
  43.         for playerId in world:query(Components.LocalPlayer) do
  44.             if playerId == tool.Owner then
  45.                 table.insert(currentOwnedTools, toolId)
  46.             end
  47.         end
  48.     end
  49.    
  50.     for playerId, player in world:query(Components.Player, Components.LocalPlayer) do
  51.         for toolId in player.StoredTools or {} do
  52.             table.insert(currentOwnedTools, tonumber(toolId))
  53.         end
  54.     end
  55.    
  56.     for _, toolId in GetItemsInserted(oldOwnedTools, currentOwnedTools) do
  57.         local toolButton = ReplicatedStorage.Client.Assets.ToolButton:Clone()
  58.         toolButton.Text = "Toggle Equip "..toolId
  59.         toolButton.Parent = state.ToolButtonContainer
  60.         state.ToolButtons[toolId] = toolButton
  61.        
  62.         print("player Owned", toolId)
  63.     end
  64.    
  65.     for _, toolId in GetItemsRemoved(oldOwnedTools, currentOwnedTools) do
  66.         local button = state.ToolButtons[toolId]
  67.         if button then
  68.             button:Destroy()
  69.         end
  70.        
  71.         state.ToolButtons[toolId] = nil
  72.         print("player Unowned", toolId)
  73.     end
  74.    
  75.     for toolId, button in state.ToolButtons do
  76.         for _ in Matter.useEvent(button, "Activated") do
  77.             ReplicatedStorage.Shared.Remotes.RequestEquipTool:FireServer(toolId)
  78.            
  79.             print("should toggle equip", toolId)
  80.         end
  81.     end
  82. end
  83.  
  84. return CreateToolButtons
  85.  
  86.  
  87.  
  88.  
  89.  
  90. local Players = game:GetService("Players")
  91. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  92.  
  93. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  94. local Components = require(ReplicatedStorage.Shared.Components)
  95.  
  96. local function ToggleEquippingTools(world, state)
  97.     for _, playerInstance, toolId in Matter.useEvent(ReplicatedStorage.Shared.Remotes.RequestEquipTool, "OnServerEvent") do
  98.         local playerId = playerInstance:GetAttribute("EntityId")
  99.         if not playerId or not world:contains(playerId) then
  100.             continue
  101.         end
  102.        
  103.         local player = world:get(playerId, Components.Player)
  104.         if not player then
  105.             continue
  106.         end
  107.        
  108.         local oldStoredTools = player.StoredTools or {}
  109.         local toolComponents = oldStoredTools[toolId]
  110.         if toolComponents then
  111.             local newStoredTools = table.clone(oldStoredTools)
  112.             newStoredTools[toolId] = nil
  113.            
  114.             world:spawn(table.unpack(toolComponents))
  115.             world:insert(playerId, player:patch({
  116.                 StoredTools = newStoredTools
  117.             }))
  118.            
  119.             continue
  120.         end
  121.        
  122.         if not world:contains(toolId) then
  123.             continue
  124.         end
  125.        
  126.         local tool = world:get(toolId, Components.Tool)
  127.         if tool.Owner ~= playerId then
  128.             continue
  129.         end
  130.        
  131.         local components = {}
  132.         for _, component in Components do
  133.             local componentInstance = world:get(toolId, component)
  134.             if componentInstance then
  135.                 table.insert(components, componentInstance)
  136.             end
  137.         end
  138.        
  139.         local newStoredTools = table.clone(oldStoredTools)
  140.         newStoredTools[toolId] = components
  141.        
  142.         world:despawn(toolId)
  143.         world:insert(playerId, player:patch({
  144.             StoredTools = newStoredTools
  145.         }))
  146.     end
  147. end
  148.  
  149. return ToggleEquippingTools
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement