-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
-- References
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local InventoryGui = PlayerGui:WaitForChild("InventoryGui")
local PrimarySlot = InventoryGui.Canvas.PrimarySlot
local SecondarySlot = InventoryGui.Canvas.SecondarySlot
local PrimaryEquipEvent = ReplicatedStorage.Events.PrimaryEquipEvent
local DeployEvent = ReplicatedStorage.Events.DeployEvent
-- Variables
local equipped = {
primary = nil :: Tool?,
secondary = nil :: Tool?,
current = nil :: Tool?
}
local COLORS = {
SELECTED = Color3.fromRGB(0, 255, 0),
UNSELECTED = Color3.fromRGB(0, 0, 0)
}
-- Types
type PrimaryData = {
Id: number,
Name: string,
ImageId: number?,
Tool: Tool?,
ObtainPart: BasePart,
ObtainMethod: string,
KillCount: number?,
BadgeId: number?,
GamepassId: number?
}
-- Private Functions
local function equipPrimary()
local character = LocalPlayer.Character
if not character then return end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid or humanoid.Health <= 0 then return end
if equipped.current == equipped.primary then
humanoid:UnequipTools()
equipped.current = nil
PrimarySlot.ImageLabel.BackgroundColor3 = COLORS.UNSELECTED
else
humanoid:EquipTool(equipped.primary)
equipped.current = equipped.primary
PrimarySlot.ImageLabel.BackgroundColor3 = COLORS.SELECTED
end
end
local function equipSecondary()
local character = LocalPlayer.Character
if not character then return end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid or humanoid.Health <= 0 then return end
if equipped.current == equipped.secondary then
humanoid:UnequipTools()
equipped.current = nil
SecondarySlot.ImageLabel.BackgroundColor3 = COLORS.UNSELECTED
else
humanoid:EquipTool(equipped.secondary)
equipped.current = equipped.secondary
SecondarySlot.ImageLabel.BackgroundColor3 = COLORS.SELECTED
end
end
-- Main Script
InventoryGui.Enabled = true
PrimaryEquipEvent.OnClientEvent:Connect(function(data: PrimaryData, isEquipped: boolean)
if isEquipped then
PrimarySlot.ImageLabel.Image = "rbxassetid://" .. data.ImageId
end
end)
DeployEvent.OnClientEvent:Connect(function()
local Backpack = LocalPlayer:WaitForChild("Backpack")
local Primary = Backpack:FindFirstChild("Primary")
assert(Primary, "Player backpack not initialized")
local PrimaryTool = Primary:FindFirstChildWhichIsA("Tool")
assert(PrimaryTool, "No primary tool in player's backpack")
local Secondary = Backpack:FindFirstChild("Secondary")
assert(Secondary, "Player backpack not initialized")
local SecondaryTool = Secondary:FindFirstChildWhichIsA("Tool")
-- assert(SecondaryTool, "No secondary tool in player's backpack")
equipped.primary = PrimaryTool
equipped.secondary = SecondaryTool
end)
LocalPlayer.CharacterRemoving:Connect(function()
equipped.current = nil
equipped.primary = nil
equipped.secondary = nil
end)
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.One then
equipPrimary()
elseif input.KeyCode == Enum.KeyCode.Two then
equipSecondary()
end
end)
PrimarySlot.MouseButton1Click:Connect(function()
equipPrimary()
end)
SecondarySlot.MouseButton1Click:Connect(function()
equipSecondary()
end)
This is my custom tool equip system.
The deploy event is fired and the tool is initially equipped only on the client side.
I want to know why this happens.
And how to fix this.