Preventing player from un-equipping Gun weapons but switch guns

–{ Introduction }–

Hello Developers!, Im DragonDarkVader! A Roblox Developer with over 1.4yrs of Building Experience!, And I’m learning Programming too.

–{ What i need help with }–
Uhm…I need help with preventing a Player from Unequipping the Gun Weapon, Instead of Unequipping it. They can Equip another gun, Which is the M4-A1. I followed the Automatic Equip Script from Developer Forum. And placed it in StarterCharacterScripts, The Main Weapon would be the M4-A1, And players can Equip another gun which is the Geist17 gun insead of Un-equipping it.

–{ The Automatic Equip Code }-

local Players = game:GetService("Players")

local player = Players:FindFirstChildOfClass("Player")
if player and player.Character then
	local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local tool = workspace:FindFirstChild("M4-A1")
		if tool then
			humanoid:EquipTool(tool)
		end
	end
end

Can someone help me add it? The Preventing from Un-equip System?

–{ Last Notes }–

Thank you very much for the Help Everyone! God Bless and Be Safe

2 Likes

One thing I usually do is this:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local equipped = "one"

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.One then
		if equipped ~= "one" then
			equipped = "one"
			humanoid:UnequipTools()
			humanoid:EquipTool(toolone)
		end
	elseif key.KeyCode == Enum.KeyCode.Two then
		if equipped ~= "two" then
			equipped = "two"
			humanoid:UnequipTools()
			humanoid:EquipTool(tooltwo)
		end
	end
end)
1 Like

As far as i know, the only ways i can think to stop tools from being unequipped, is either to not use tools, and instead make your own tool system (which is actually pretty easy) or to have a line of code that runs when the tool is unequipped, that then checks the character for another tool, if there isnt a tool, then the tool will re-parent itself to the character.
I will not supply code for a custom tool system, as it is long and its my system im using on a game, plus im bad at scripting, but i can provide you with the automatic re-equip script.

– forgive any errors with tools here, i dont use them at all anymore, and tend to make my own systems.

tool.Unequipped:Connect(function()
task.wait()
if not character:FindFirstChildOfClass("Tool") then
tool.Parent = character
end
end)
1 Like

where do i put the Local Script in?

ErrorScript

you have to define the player’s humanoid

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

as for the toolone and tooltwo you have to put tool instance in the first parameter of humanoid:EquipTool()

So where do i put that part? (character limit)

top of the script for most of the time

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
--rest of the code
1 Like

i got an Error

Error3

toolone is the First tool object from somewhere like ReplicatedStorage, and same with tooltwo.

So i place the weapons in a Folder inside Replicated Storage?

Something like that

1 Like

So in a two Separate folders? (character limit)

Folder
this?

Something similar, it doesn’t matter much as long as toolone is the first tool instance and the same with tooltwo.

It show’s the same, Orange Line in toolone, tooltwo

What @BriefYayyayyay means is that the items are in replicated storage, and you have to define them in the code, as you did with the player, character and humanoid.
(In this code, there is a folder in ReplicatedStorage called “Tools” and your guns are inside that folder)

local toolone = game.ReplicatedStorage.Tools["M4-A1"]
local tooltwo = game.ReplicatedStorage.Tools["Geist17"]

Where do i add that part to this script?

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
--rest of the code

local equipped = "one"

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.One then
		if equipped ~= "one" then
			equipped = "one"
			humanoid:UnequipTools()
			humanoid:EquipTool(toolone)
		end
	elseif key.KeyCode == Enum.KeyCode.Two then
		if equipped ~= "two" then
			equipped = "two"
			humanoid:UnequipTools()
			humanoid:EquipTool(tooltwo)
		end
	end
end)

Right under where the Humanoid, Character and Player are defined.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local toolone = game.ReplicatedStorage.Tools:WaitForChild("M4-A1")
local tooltwo = game.ReplicatedStorage.Tools:WaitForChild("Geist17")

Im also going to put this here, so you dont get messed up with the folders.
Its just a screenshot of what the Tools folder in ReplicatedStorage would be like.
Screenshot (602)

1 Like

By the way, I’ll place it to StarterPlayerScripts?