E to equip weapon aint working

Hello!

I’ve been having some trouble with my E to Equip script that is located inside of the weapon. I want it so the when the player press e, their weapon will be equipped but will not show in backpack GUI. To do this, I set a LocalScript inside of StarterGUI that will disable the GUI. But once the player loads in, and the default roblox GUI shows, the backpack appears.

E to Equip Script:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Animation = game:GetService("Animation")

local player = Players.LocalPlayer
local toolName = "Basic Nichirin"
local ToolForEquipping = player.Backpack:FindFirstChild(toolName)

local equipAnimationId = "rbxassetid://18684966224"
local equipAnimation = Instance.new("Animation")
equipAnimation.AnimationId = equipAnimationId

local function playEquipAnimation(character)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local animator = humanoid:FindFirstChildOfClass("Animator")
		if not animator then
			animator = Instance.new("Animator")
			animator.Parent = humanoid
		end
		local animationTrack = animator:LoadAnimation(equipAnimation)


		local originalWalkSpeed = humanoid.WalkSpeed
		humanoid.WalkSpeed = 0


		animationTrack:Play()


		animationTrack.Stopped:Connect(function()
			humanoid.WalkSpeed = originalWalkSpeed
		end)
	end
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if ToolForEquipping then
			if ToolForEquipping.Parent == player.Backpack then

				player.Character.Humanoid:EquipTool(ToolForEquipping)
				playEquipAnimation(player.Character)
			elseif ToolForEquipping.Parent == player.Character then

				player.Character.Humanoid:UnequipTools()
			end
		end
	end
end)

Disable GUI script:

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

Video Showing Issue

1 Like

There’s a chance that the CoreGui hasn’t loaded yet by the time you try to disable it, meaning when it does load, it isn’t disabled. Try repeatedly disabling it until it is disabled.

local StarterGui = game:GetService("StarterGui")

repeat
	task.wait()

	local Success, ErrorMessage = pcall(function()
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	end)

until Success
1 Like

the same thing still happens :confused:

Your code should work. Is there any scripts that re-enable the backpack?