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)
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:
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)
.