Unresponsive UIS function

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)
3 Likes

Try this and see if anything prints out:


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)
1 Like

Script got stuck at line 2, output says Players.SpotInvigo.Backpack.Mace.LocalScript:2: attempt to index nil with ‘WaitForChild’

1 Like

Well in that case, it means that the variable Character is nil. (Updated, forgot to add the :Wait(), thanks @AridFights1)

Try replacing it with:

local Character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()

You forgot the :Wait() at the end.

1 Like

Your script works, for me. (Replaced the slash animation with one of my earlier test animations.)

Should work for you too, @SpotInvigo.

Done, EQUIP IS NOT TREE warn comes out whenever I click.

1 Like

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.

1 Like

Did you click while the tool is equipped?

Obviously, but it’s unresponsive.

Maybe try tool.Activated instead of UIS.InputBegan?

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

Haha, you got some good eyes! Didn’t notice that at all!

1 Like

I don’t know how I didn’t notice that earlier, thank you very much.

1 Like

I’ve made the same mistake many times…

Glad I could help.

1 Like

Oh wow, that completely went off my radar-
I can’t believe I didn’t realize that.

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)

https://developer.roblox.com/en-us/api-reference/event/Tool/Activated

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.

Console support, GameProcessedEvent, IsKeyDown, and overall properties and events UIS has that allows much more than just a click.