Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Tool not activating even when equipped?

Asked by 3 years ago

Ok, so I'm working on a (kind of) overpowered tool. But for some reason, the tool isn't working it is displaying with the error message

Tool:Activate() called from script when tool is not equipped. Tool will not be activated.

even when the tool is equipped. This warning should only happen when the tool isn't equipped...

Code for the tool:

local UIS = game:GetService("UserInputService")
local action = ""
local hum = script.Parent.Parent.Parent.Character.Humanoid
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()

UIS.InputBegan:Connect(function(input, istyping)
    local action 
    if not istyping and input.KeyCode == Enum.KeyCode.X then
        action = "Teleport"
        tool:Activate()
    elseif  input.KeyCode == Enum.KeyCode.R then
        action = "Donut"
        tool:Activate()
    elseif input.KeyCode == Enum.KeyCode.V then
        action = "KC"
        tool:Activate()
    elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
        action = "Punch"
        tool:Activate()
        end
end)

tool.Activated:Connect(function()
    print(action)
    if action == "Teleport" then
        hum.Parent:MoveTo(mouse.Hit.Position)

    end
end)


1 answer

Log in to vote
0
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

Problem:

It really helps when you organize your code. You had a few issues in your code. I outlined some errors below as comments:

local UIS = game:GetService("UserInputService")
local action = ""
local hum = script.Parent.Parent.Parent.Character.Humanoid -- Player and character should be variabled
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse() -- Player could be a variable

UIS.InputBegan:Connect(function(input, istyping)
    local action -- This doesn't need to be here, it is overriding the previously defined 'action' making it irrelevant 

In your code, you should try to make a variable whenever you have to reference the same thing multiple times within the same scope.

Solution:

  • Organize your code. It is useful to separate your code into different sections that share a common theme.
  • Prevent unnecessary references by referencing once and storing the value in a variable.
  • Paying attention to the scope and not overriding needed variables.

Fixed code:

This code was tested in Studio. It will work when placed in a LocalScript as a direct child of the tool.

--=== Services ===--

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

--=== Variables ===--

local action = ""

local Player = Players.LocalPlayer
local Character = Player.Character
repeat Character = Player.Character wait() until Character ~= nil

local hum = Character.Humanoid

local tool = script.Parent
local mouse = Player:GetMouse()

--=== Hooks (Connections) & Main Loop ===--

UIS.InputBegan:Connect(function(input, istyping)
    if not istyping and input.KeyCode == Enum.KeyCode.X then
        action = "Teleport"
        tool:Activate()
    elseif  input.KeyCode == Enum.KeyCode.R then
        action = "Donut"
        tool:Activate()
    elseif input.KeyCode == Enum.KeyCode.V then
        action = "KC"
        tool:Activate()
    elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
        action = "Punch"
        tool:Activate()
    end
end)

tool.Activated:Connect(function()
    print(action)
    if action == "Teleport" then
        hum.Parent:MoveTo(mouse.Hit.Position)

    end
end)

Conclusion:

If you have any questions, or any more errors, you can contact me via Discord (phxntxsmic#2021).

Ad

Answer this question