Help with userinputservice

Hello, I was making a lightsaber script and trying to make light attacks and a block script for it.
I made the script but the userinputservice does not register any key nor does the blocking , light attacks are working. I have no idea whats wrong with it, I tried printing but it only goes up to userinputserviceworkomg print and doesnt go forward then that.

wait()
local rem = game.ReplicatedStorage.playa
rem.OnServerEvent:Connect(function(player)
local tool = script.Parent
local Equipanimation = script.EquipAnimation
local UnequipAnimation = script.UnequipAnimation
local humanoid
local character = player.Character
local Equip = script:WaitForChild("Sound")
local unEquip = script:WaitForChild("Sound2")
	local block = Instance.new("BoolValue",player.Character)
	block.Name = "Block"
local delay = 0.3

tool.Equipped:Connect(function()
	humanoid = tool.Parent:FindFirstChild("Humanoid")
	local loader = humanoid:LoadAnimation(Equipanimation)
	loader:Play()
	Equip:Play()
	print("Equippped")

	--//Attacking Animations\\--
	local UserInputService = game:GetService("UserInputService")
	local Tool = script.Parent
	local CanAttack = true
	local Anim_Name = "Attack"
	local Attack_Num = 1

	local blade = script.Parent.Handle --(update this to where the sword is)

	blade.Touched:Connect(function(hit)
			if CanAttack == false and hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent.Block == false then
					hit.Parent.Humanoid:TakeDamage(15)
				end
				
		end
		end)
		local UIS = game:GetService("UserInputService")
		local held = false
print("userinputworkomg")
		UIS.InputBegan:Connect(function(input)
			print("Input happned")
			if input.UserInputType == Enum.UserInputType.MouseButton2 then
				print("mouse pressed")
				held = true
				while held == true do
					print("blocking")
					local block1 = Instance.new("Animation")
					block1.AnimationId = "rbxassetid://6512534347"
					local blockanim1 = script.Parent.Parent.Humanoid:LoadAnimation(block1)
					blockanim1:Play()
					local blockau1 = Instance.new("Sound")
					blockau1.SoundId = "rbxassetid://6512152579"
					block.Value = true
					game:GetService("RunService").RenderStepped:Wait()
				end
			end
		end)

		UIS.InputEnded:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton2 then
				held = false
				block.Value = false
			end
		end)
			local function onInputBegan(input, gameProcessed)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					local swing1 = Instance.new("Animation")
					swing1.AnimationId = "rbxassetid://6512534347"
					local Animation1 = script.Parent.Parent.Humanoid:LoadAnimation(swing1)
					local swing2 = Instance.new("Animation")
					swing2.AnimationId = "rbxassetid://6512534347"
					local Animation2 = script.Parent.Parent.Humanoid:LoadAnimation(swing2)
					if CanAttack == true then
						CanAttack = false
						Animation1:Play()
						local Audio1 = Instance.new("Sound")
						Audio1.SoundId = "rbxassetid://6512152579"
						local Audio2 = Instance.new("Sound")
						Audio2.SoundId = "rbxassetid://536642316"
						Audio1:Play()
						-- CanAttack = false (moved this to above the playing of the anims, to make sure it is already false when the onTouched is triggered
						wait(1)
						CanAttack = true
						if Attack_Num == 1 then
							character:PivotTo(character:GetPivot() * CFrame.new(0, 0, -7))
							Attack_Num = Attack_Num + 1
							print("Attack1")
						else if Attack_Num == 2 then
								character:PivotTo(character:GetPivot() * CFrame.new(0, 0, -7))
								Attack_Num = 1
								Audio2:Play()
								Animation2:Play()
								print("Attack2")
							end
						end
					end
				end
			end

			UserInputService.InputBegan:Connect(onInputBegan)

		end)
tool.Unequipped:Connect(function()
	local loader = humanoid:LoadAnimation(UnequipAnimation)
	loader:Play()
	unEquip:Play()

	print("Unequipped")
	end)
	end)

Is the code a LocalScript and do you get any errors in the output?

2 Likes

It is, see how he got the Player?

This is clearly a server script.

Oh, I didn’t see that I just looked at the Player So I thought he got the Player from the local script

UserInPutService doesn’t happen on the Server it happens on the Client So use local script for it.

UserInputService is a client sided service. You must make your code a LocalScript

It is a server script guys charssssssssssss

Yeah and that’s the problem, like @Z_lqify said. UserInputService can only be used on the client (aka in LocalScript)

So make it client sided or it won’t work yk?