How to control when a player can switch/unequip tools

Im working on editing a gun kit, and wanted to know if this is possible and how, thats all i need to know

Its possible, don’t know what gun kit you using but it is possible

Im editing the “trench warfare shotgun” to make it a gun kit

1 Like

Ooh, then yeah its very possible

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Value = ServerStorage.Value

local Connections = {}

Value.Changed:Connect(function(NewValue)
	if NewValue then
		for _, Player in ipairs(Players:GetPlayers()) do
			local Character = Player.Character
			local Humanoid = Character.Humanoid
			local EquippedTool
			
			for _, Child in ipairs(Character:GetChildren()) do
				if Child:IsA("Tool") then
					EquippedTool = Child
				end
			end
			
			local Connection1
			Connection1 = Character.ChildAdded:Connect(function(NewChild)
				if NewChild:IsA("Tool") then
					EquippedTool = NewChild
					Connection1:Disconnect()
				end
			end)
			
			local Connection2
			Connection2 = Character.ChildRemoved:Connect(function(OldChild)
				if OldChild == EquippedTool then
					Humanoid:EquipTool(EquippedTool)
				end
			end)
			table.insert(Connections, Connection2)
		end
	elseif not NewValue then
		for _, Connection in ipairs(Connections) do
			if Connection.Connected then
				Connection:Disconnect()
			end
		end
	end
end)

--Testing.
task.wait(5)
Value.Value = true
task.wait(5)
Value.Value = false

Memory leak free! Anyway, this implementation relies on a “BoolValue” instance stored inside the “ServerStorage” folder. When its “Value” property is assigned a value of true, tools cannot be switched between or unequipped. If the value is set before some character equips a tool then they will still be able to equip a tool (however they will be unable to switch from/unequip that tool after the fact). When its “Value” property is set to false, players will be able to switch between and unequip their tools freely.

As previously mentioned this implementation makes use of a “BoolValue” instance, this can be switched out in favor of a state variable/flag instead.

Proof:
https://gyazo.com/5b72c91566a545a83f92b1c8cf744384