Tool:Activate() won't work

So I made a robot that follows you and has a tool, the only thing is that when I do Tool:Activate() the tool won’t activate.
Here’s an example; https://gyazo.com/d921ceaec0820428fda236ee9042fd3b
Here’s my script:

		if Mag <= 10 then
			saber:Activate()
		end

Mag = magnitude of the robot’s torso and the target’s torso.
saber = the saber the robot has equipped.
Also, the magnitude works just fine. When I insert a print() to see if the magnitude works, it works.
Can anyone help?

2 Likes

According to documentation, Tool:Activate() only works if the tool is equipped. My assumption is that for a tool to be counted as equipped on an NPC, Humanoid:EquipTool() must be used.

Are you using this function? If not try using it and see if your code functions as intended. If you are and it still doesn’t work try using BindableEvents and having your callback connect to the bindable event. Then you can bind Tool.Activated to fire this event. And finally to trigger it in other code you can fire the bindable event.

1 Like

Yep, I’m using this piece of code to equip it.

local Saber = game.Lighting.Storage["Licht Zwaard"]:Clone()
Saber.Parent = script.Parent
script.Parent.Saber.Value = Saber
Humanoid:EquipTool(Saber)

In that case the Activate function may only work for player characters. I can’t find that documented somewhere but it probably is if it is not working properly in an NPC. Otherwise this is probably a bug of some kind. If you try the functions for a player character does it work?

One other issue that might be happening is that you aren’t updating your Mag variable. Try printing something when you call :Activate to make sure it is being called.

NPCs, at least in my knowledge, cant call Tool:Activate() or Tool:Deactivate(). Activation must be called from either a Local or Server sided script that is a descendant of the player or player character. Instead of doing something like:

t:Activated:Connect(function()
end)

Do:

function func()
end

t.Activated:Connect(func)

That way, it can be easier to have bots perform certain actions, without creating too much complication. When using a tool under an NPC, also keep in mind what player-actions can’t be performed, but rather simulated. As dumb as it sounds, just read through the tool API under the roblox developer hub to get an idea of what it can and cannot do.

Hope that helped :wink:

4 Likes

Personally I use BindableEvents for situations like this. It allows for external code to fire it as well which is usually the case.

2 Likes