Tool.Enabled does not work?

I thought Tool.Enabled = false was supposed to prevent Tool.Activated from firing. Why can this assertion fail though?

-- LocalScript
local Tool = script.Parent
Tool.Activated:Connect(function()
	assert(Tool.Enabled)
end)
1 Like

In the code sample you’ve provided, you haven’t changed the value of Tool.Enabled

The assert global is used to throw an error if a value is equal to false, in your code sample nothing will be shown in the output as Tool.Enabled is true.

Perhaps you could provide some more information about what you’re trying to achieve here?

1 Like

According to Tool | Roblox Creator Documentation

The Enabled property relates to whether or not the Tool can be used. This is useful if you want to prevent a player from using a tool, but don’t want to remove it from their Backpack.

When set to true, the player can use the tool. When set to false, the tool is disabled and the player cannot use it; this prevents the tool from being activated or deactivated by the Tool:Activate() and Tool:Deactivate() methods, and it prevents the Tool.Activated and Tool.Deactivated events from firing.

Ostensibly, if a Tool.Enabled is false, the Tool.Activated event should not fire, so the assert would not be called. However, the assert failing would mean that Tool.Activated is firing even though Tool.Enabled is false, contrary to what the page says. My problem just that. Tool.Activated is firing even though Tool.Enabled is false, as demonstrated by the fact that assertion failed messages are printed to the console if I set Tool.Enabled to false and then try to use the tool.

Please show me the code sample where you’ve set the tool.Enabled property to false

That would be unnecessary. Even if I uncheck Enabled from the studio before clicking Play, Activated events are still being received.

Try this. It may work?

local Tool = script.Parent

Tool.Activated:Connect(function()
	local success, err = pcall(function()
        assert(Tool.Enabled)
    end)
end)

The error could be caught, or I could change the assert(...) to an if not ... then return end but I want to know why Tool.Activated is firing while Tool.Enabled is false in the first place.

Definitely a strange one.

You could move the assert(Tool.Enabled) above Tool.Activated preventing it from firing. This could remind the script that Tool.Enabled is false and it wont run the Tool.Activated event?

You could also try checking if Tool.Enabled is false or true and depending on the result you can either stop the activated event from firing further, or you can fire some extra code?

local Tool = script.Parent

Tool.Activated:Connect(function()
	if assert(Tool.Enabled) then
        print("activated")
    else
        break
    end
end)

it’s pretty vague. ALSO HOLY IT’S HIM! the post should have been more clear

2 Likes

ALSO HOLY IT’S HIM!

Who is it? Also, one of my replies states the problem more clearly.

Gnome code. YouTuber. He basically taught me ai lol. Also thanks for clarifying! There’s also a post where it says that assert is bad due to it being just slow