Make Player Equip Tool On Button Press

Hello everyone! I’m new here, and I’ve been trying to make a custom hotbar, I’ve disabled the default backpack GUI, so I can’t equip tools with the 0-9 keys, and I’ve been trying to recreate that so that if I press 1, it equips a gun that is in the StarterPack, here is what I’ve scripted so far.

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Backpack = Player:WaitForChild("Backpack")
local Gun = Backpack:FindFirstChild("Gun")

UserInputService.InputBegan:Connect(function(input, process)
	if not process then
		if input.KeyCode == Enum.KeyCode.One then
			if Humanoid then
				if Gun then
			Humanoid:EquipTool(Gun)
		end
		end
	end
end
end)

However, this doesn’t work, I know that the button press event works because I placed a breakpoint and it stopped the test, but the player doesn’t equip the tool, if anyone can offer help I would be really grateful.

5 Likes

You could make a system where it detects if it is enabled or not. You could use RemoteEvents, if the tool is inside of the character it counts as equipped.

1 Like

Sorry, I didn’t understand what you meant by that, did you mean using RemoteEvents to check if the tool is enabled or not?

You could use RemoteEvents to check if the gun is equipped or not.

Edit: Perhaps you could use a BoolValue to detect if the gun has been equipped or not.

And as usually asked, I am assuming that is in a LocalScript?

Yes, I put it in a LocalScript.

Give me a moment while I try to rewrite your code.

I have rewritten your code the way I would’ve made it and I have fixed a few of your errors and removed unnecessary parts.

Put this in the LocalScript:

    local Service = game:GetService("UserInputService")

    Service.InputBegan:Connect(function(KeyboardInput)
    	if KeyboardInput.KeyCode.Name == "One" then
    		game.ReplicatedStorage:WaitForChild("GunEquip"):FireServer()
    	end
    end)

Make a new script in ServerScriptService and paste this:

            local Event = Instance.new("RemoteEvent")
            Event.Name = "GunEquip"
            Event.Parent = game.ReplicatedStorage
            Equipped = false

            Event.OnServerEvent:Connect(function(Player)
            	if Equipped == false then
            		Player.Backpack:WaitForChild("Gun").Parent = Player.Character
            		Equipped = true
            	else
            		Player.Character:WaitForChild("Gun").Parent = Player.Backpack
            		Equipped = false
            	end
            	Player.Character.Humanoid.Died:Connect(function()
            		Equipped = false
            	end)
            end)
12 Likes

Thank you! It worked perfectly.

3 Likes

I found another solution which does not require a server script. I don’t know if it is good to have the server handle the equipping of tools, so be sure to look into that.

local Character = Player.CharacterAdded:Wait()

When I tested it, it kept waiting, and I don’t think Character would be set to anything anyway.

2 Likes

Very late, but when i was using this only one player can equip the tool. I have no idea how to fix this but I have been trying by making it so the equipped varible is sent on the client. But whatever i try does not work.

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local isequiped = false

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.F then
		if isequiped == false then
			isequiped = true
			game.ReplicatedStorage.FlashlightEquip:FireServer(plr, isequiped)
		else
			isequiped = false
			game.ReplicatedStorage.FlashlightEquip:FireServer(plr, isequiped)
		end
	end
end)

local event = game.ReplicatedStorage.FlashlightEquip

event.OnServerEvent:Connect(function(plr, isequiped)
	if isequiped == false then
		plr.Backpack:WaitForChild("Flashlight").Parent = plr.Character
		isequiped = true
	else
		plr.Character:WaitForChild("Flashlight").Parent = plr.Backpack
		isequiped = false
	end
	plr.Character.Humanoid.Died:Connect(function()
		isequiped = false
	end)
end)
1 Like

Any fix to this? Players in my game can’t equip the tool if someone already has it equipped

I rescripted a fix… I lost the script…

I got a working version

ServerScriptService

local Event = Instance.new("RemoteEvent")
Event.Name = "TorchEquip"
Event.Parent = game.ReplicatedStorage

local equippedStatus = {}

Event.OnServerEvent:Connect(function(Player)
	if equippedStatus[Player] == nil or not equippedStatus[Player] then
		Player.Backpack:WaitForChild("Torch").Parent = Player.Character
		equippedStatus[Player] = true
	else
		Player.Character:WaitForChild("Torch").Parent = Player.Backpack
		equippedStatus[Player] = false
	end

	Player.Character.Humanoid.Died:Connect(function()
		equippedStatus[Player] = false
	end)
end)

StarterGui

local Service = game:GetService("UserInputService")

Service.InputBegan:Connect(function(KeyboardInput)
	if KeyboardInput.KeyCode.Name == "F" then
		game.ReplicatedStorage:WaitForChild("TorchEquip"):FireServer(game.Players.LocalPlayer)

	end
end)
1 Like