Need help with a lightsaber script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    A blocking lightsaber
  2. What is the issue? Include screenshots / videos if possible!
    My inputservice “IsKeyDown” says “unable to cast token to token”
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes, didnt work
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local BlockCooldown = false
local UserInputService = game:GetService("UserInputService")

local Tool = script.Parent
local Animation = script:WaitForChild("swing1")

local Equipped

Tool.Equipped:Connect(function()
	Equipped = true
end)

Tool.Unequipped:Connect(function()
	Equipped = false
end)

UserInputService.InputBegan:Connect(function(Input, Processed) -- attack
	if Processed then return end
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then		
		if Equipped then
			local player = script:FindFirstAncestorWhichIsA"Player" or game:GetService"Players":GetPlayerFromCharacter(script.Parent.Parent)
			local plrhuman = player.Character:FindFirstChild("Humanoid")
			local anim = script.swing1
			local setanim = anim:Clone()
			setanim.Parent = plrhuman
			local animtrack = plrhuman:LoadAnimation(setanim)
			animtrack:Play()
		end		
	end		
end)

UserInputService.InputEnded:Connect(function(input) -- detect if block button is up

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
	if holdingblock == false then return end	
	holdingblock = false
	end

end)


UserInputService.InputEnded:Connect(function(Input, Processed)
if Processed then return end		
if Input.UserInputType == Enum.UserInputType.MouseButton2 then
holdingblock = false
end

	if Input.UserInputType == Enum.UserInputType.MouseButton2 then -- block
		if Equipped then -- second
			holdingblock = true
			local player = script:FindFirstAncestorWhichIsA"Player" or game:GetService"Players":GetPlayerFromCharacter(script.Parent.Parent)
			local plrhuman = player.Character:FindFirstChild("Humanoid")
			local anim = script.block1
			local setanim = anim:Clone()
			setanim.Parent = plrhuman
			local animtrack = plrhuman:LoadAnimation(setanim)
			print("it played but... nothing?")
			animtrack:Play()
	animtrack.Stopped:Connect(function()
		while true do	
			wait()		
				
			if UserInputService:IsKeyDown(Enum.UserInputType.MouseButton2) then
					animtrack:Play()
						repeat wait()
							
						until animtrack.Stopped == true		
				elseif holdingblock == false then break end		
					
			end
		BlockCooldown = true	
		wait(5)		
		BlockCooldown = false
	end)
					


			end

		end
end)

It’s because u need to put the keycode in the parameter instead of input type
this would prob help u UserInputService:IsKeyDown (roblox.com)

And since there is no keycode for mouse button 2, u will prob have to check when player pressed the mouse button 2 and when he let it go n just make a debounce so that when the mouse button 2 is held it goes true and when its not it goes false

https://developer.roblox.com/en-us/api-reference/function/UserInputService/IsMouseButtonPressed

1 Like