I am trying a very simple sword animation script, the function isn’t doing its thing and I can’t seem to spot what I did wrong, as you can see, it makes sure it meets all conditions.
wait()
local Character = game.Players.LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Debounce = false
local Equip = false
script.Parent.Equipped:Connect(function()
local Equip = true
print("Equip")
end)
script.Parent.Unequipped:Connect(function()
local Equip = false
print("Unequip")
end)
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if Equip == true then
if not gameProcessedEvent then
if not Debounce then
Debounce = true
print("Attack")
local Slash = Humanoid:LoadAnimation(Character:FindFirstChild("Animations").Slash)
Slash:Play()
wait(1)
Debounce = false
end
end
end
end
end)
local Character = game.Players.LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Debounce = false
local Equip = false
script.Parent.Equipped:Connect(function()
local Equip = true
print("Equip")
end)
script.Parent.Unequipped:Connect(function()
local Equip = false
print("Unequip")
end)
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if Equip == true then
if not gameProcessedEvent then
if not Debounce then
Debounce = true
print("Attack")
local Slash = Humanoid:LoadAnimation(Character:FindFirstChild("Animations").Slash)
if Slash ~= nil then
Slash:Play()
else
warn("SLASH IS NIL")
end
task.wait(1)
Debounce = false
else
warn("DEBOUNCE == TRUE")
end
else
warn("GAMEPROCESSEDEVENT == TRUE")
end
else
warn("EQUIP IS NOT TREE")
end
else
warn("USER INPUT TYPE IS NOT MOUSE BUTTON 1")
end
end)
That is odd, I even did some testing and added prints to check every condition was met, it never printed out after the if Equip == true then conditional, so I assume that’s the problem.
In your equipped and unequipped events you have local Equip = true
this will make a new LOCAL variable only in the scope of that event.
You do not want this.
Change it to Equip = true (get rid of the local) in both cases.
This is making it so that your UIS event never gets past the if Equip == true
I guess it needs to be stated that the “Activated” event exists, which means that you don’t need to make use of the tool’s equipped/unequipped events nor the input events of the UserInputService. Simply put the activated event fires on a tool instance when that tool is both equipped and the left mouse button is pressed.
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animations = Character:WaitForChild("Animations")
local SlashAnim = Animations:WaitForChild("Slash")
local SlashTrack = Humanoid:LoadAnimation(SlashAnim)
repeat task.wait() until SlashTrack.Length > 0
local Tool = script.Parent
local Debounce = false
Tool.Activated:Connect(function()
if Debounce then
return
end
Debounce = true
SlashTrack:Play()
task.wait(1)
Debounce = false
end)
As a general rule of thumb, try to declare all of your variables outside of the callback functions connected to your events, this way each time those callback functions are executed they do not end up re-declaring the same constants (variables with unchanging values).
I am aware of the Activated event, however, I had understood that UserInputService is a far more advanced alternative to this and also Player:GetMouse(), which is the reason I even started programming this tool, as it’d allow me to implement more features and have a little more control over it, resigning to use Activated would only defeat its purpose. Thank you anyway.
What features and what control would this far more difficult implementation provide? Most if not all of Roblox’s gear make use of the “.Activated” event, I believe you may have been misinformed.