Why won't my tool equip when I press a key?

So I have a flashlight tool. When you press F, the flashlight should be equipped. But it isn’t working. I’ve figured out that it’s due to me not having CharacterAutoLoads on, but I’m not sure what the issue is.

LocalScript in StarterPlayerScripts:

local ContextActionService = game:GetService('ContextActionService')
local Players = game:GetService('Players')
local sound = script:WaitForChild("equip1")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = game.StarterPack.Flashlight

	local function equipAction(name, state, obj)
		if state == Enum.UserInputState.Begin then
		if character:FindFirstChild(tool.Name) then
			character.Humanoid:UnequipTools()
		else
			character.Humanoid:EquipTool(tool)
			sound:Play()
		end
	end
	end

ContextActionService:BindAction(
	'equipKeybind', 
	equipAction, 
	false, 
	Enum.KeyCode.F
)

Help!

Also, the equip sound does play when I press F. So it’s a problem with actually equipping the tool.

is there any error in the output?

Nope, no errors in the output. Very confused rn

Try using UserInputService perhaps?

oh nvm, you said that the sound is playing.

Try debugging by using print statements under the if statements in the equipAction function.

try this

local sound = script:WaitForChild("equip1")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = game.StarterPack.Flashlight
local UserInputService = game:GetService("UserInputService")
	UserInputService.InputBegan:Connect(function(input,gp)
         if gp then return end
        if input.KeyCode == Enum.KeyCode.F then
		if character:FindFirstChild(tool.Name) then
			character.Humanoid:UnequipTools()
		else
			character.Humanoid:EquipTool(tool)
			sound:Play()
		end
	end
	end)

That doesn’t seem to be working

You capitalized the P in “game:GetService(“UserInputService”)”

oh ye mb, @Batmanissupercoolbro try again

even tho im not sure if it will work cuz you already said that the sound already played

Also, try waiting for the character’s humanoid before declaring it.

The new script does seem to be working, but it’s still not equipping the flashlight. The sound is playing.

what should i type to do that?

can’t you just do

tool.Parent = character

oh ye you’re getting the flashlight from StarterPack

You’re getting the flashlight from the StarterPack, try getting it from the player’s character or backpack by using script.Parent or some alternative.

1 Like

Its because you are taking the tool from StarterPack, client cant access that, change it to Backpack.

Yep. That’s worked. Thanks everyone, appreciate it!

I don’t mean to be rude but uh, am I just semi-ignored in the solution because someone beautified my text by using markdowns which makes it stand out more? Because it would seem that even though I answered first, I take no credit. Also, going back I would recommend to use character:WaitForChild(“Humanoid”):EquipTool() and character:WaitForChild(“Humanoid”):UnequipTools to wait for the character’s humanoid. You can see more information on WaitForChild here: Instance:WaitForChild. Also, ContextActionService is totally preferenced but for future reference I recommend using UserInputService as its easier in my opinion. Also, even though it was wrong to use StarterPack as seen by the solution, it also seems wrong not to use game:GetService(“StarterPack”) rather than game.StarterPack when you used GetService for getting the Players and ContextActionService service’s. Even though it seems wrong to discuss credit, seeing that mile didn’t know that I was gonna post a solution before them, it just angers me for some odd reason to see someone mark the solution for someone that posted after I posted about my take. Also, if you marked mile’s solution because of it being more clear on how to change StarterPack to Backpack, then I recommend to learn some basics of programming (don’t mean to be rude here either). I’m probably going to regret saying this seeing as I will probably be reported even though I think that I did absolutely nothing wrong. I know you’re most likely a newbie programmer/coder/scripter and didn’t realize this or see my post, but I just couldn’t help but talk open about it. Other than that, I feel that theres no more to discuss on this topic, and I hope that everybody is having a good day. P.S. I learned how to program by watching tutorials such as tutorials made by AlvinBlox and TheDevKing. Also, please do not start an argument over this.

Sorry dawg, I actually should’ve given you the solution cuz I did end up using script.Parent. I know I debated on who to give the solution to, and I’ll redo it and give you it. I kinda just feel llike you might be getting upset over useless internet points, and that mighta been a bit over the top.