Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local UserInputService = game:GetService("UserInputService")
- local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
- local Components = require(ReplicatedStorage.Shared.Components)
- local Constants = require(ReplicatedStorage.Shared.Constants)
- local GetItemsInserted = require(ReplicatedStorage.Shared.Utils.GetItemsInserted)
- local GetItemsRemoved = require(ReplicatedStorage.Shared.Utils.GetItemsRemoved)
- local localPlayerInstance = Players.LocalPlayer
- local function CreateToolButtons(world, state)
- if not state.HasInitializedToolButtons then
- state.HasInitializedToolButtons = true
- local toolButtonContainer = ReplicatedStorage.Client.Assets.ToolButtonContainer:Clone()
- world:spawn(
- Components.Canvas(),
- Components.Transform({
- Position = Vector2.new(Constants.VirtualSize.X / 2, Constants.VirtualSize.Y - 32),
- }),
- Components.Frame({
- ZIndex = 1000,
- Size = Vector2.new(16, 16),
- Instance = toolButtonContainer,
- })
- )
- state.ToolButtonContainer = toolButtonContainer
- state.ToolButtons = {}
- state.OwnedTools = {}
- end
- local oldOwnedTools = state.OwnedTools
- local currentOwnedTools = {}
- state.OwnedTools = currentOwnedTools
- for toolId, tool in world:query(Components.Tool) do
- for playerId in world:query(Components.LocalPlayer) do
- if playerId == tool.Owner then
- table.insert(currentOwnedTools, toolId)
- end
- end
- end
- for playerId, player in world:query(Components.Player, Components.LocalPlayer) do
- for toolId in player.StoredTools or {} do
- table.insert(currentOwnedTools, tonumber(toolId))
- end
- end
- for _, toolId in GetItemsInserted(oldOwnedTools, currentOwnedTools) do
- local toolButton = ReplicatedStorage.Client.Assets.ToolButton:Clone()
- toolButton.Text = "Toggle Equip "..toolId
- toolButton.Parent = state.ToolButtonContainer
- state.ToolButtons[toolId] = toolButton
- print("player Owned", toolId)
- end
- for _, toolId in GetItemsRemoved(oldOwnedTools, currentOwnedTools) do
- local button = state.ToolButtons[toolId]
- if button then
- button:Destroy()
- end
- state.ToolButtons[toolId] = nil
- print("player Unowned", toolId)
- end
- for toolId, button in state.ToolButtons do
- for _ in Matter.useEvent(button, "Activated") do
- ReplicatedStorage.Shared.Remotes.RequestEquipTool:FireServer(toolId)
- print("should toggle equip", toolId)
- end
- end
- end
- return CreateToolButtons
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
- local Components = require(ReplicatedStorage.Shared.Components)
- local function ToggleEquippingTools(world, state)
- for _, playerInstance, toolId in Matter.useEvent(ReplicatedStorage.Shared.Remotes.RequestEquipTool, "OnServerEvent") do
- local playerId = playerInstance:GetAttribute("EntityId")
- if not playerId or not world:contains(playerId) then
- continue
- end
- local player = world:get(playerId, Components.Player)
- if not player then
- continue
- end
- local oldStoredTools = player.StoredTools or {}
- local toolComponents = oldStoredTools[toolId]
- if toolComponents then
- local newStoredTools = table.clone(oldStoredTools)
- newStoredTools[toolId] = nil
- world:spawn(table.unpack(toolComponents))
- world:insert(playerId, player:patch({
- StoredTools = newStoredTools
- }))
- continue
- end
- if not world:contains(toolId) then
- continue
- end
- local tool = world:get(toolId, Components.Tool)
- if tool.Owner ~= playerId then
- continue
- end
- local components = {}
- for _, component in Components do
- local componentInstance = world:get(toolId, component)
- if componentInstance then
- table.insert(components, componentInstance)
- end
- end
- local newStoredTools = table.clone(oldStoredTools)
- newStoredTools[toolId] = components
- world:despawn(toolId)
- world:insert(playerId, player:patch({
- StoredTools = newStoredTools
- }))
- end
- end
- return ToggleEquippingTools
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement