UserInputService not working

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a block system whenever the player presses f. It doesn’t work after the player resets or die

  2. What is the issue? Include screenshots / videos if possible!
    It doesn’t work anymore after resetting.
    https://gyazo.com/d0f8a2d3080de1ae7b4a416eed24fea1

i was press f and it doesn’t work
https://gyazo.com/6a183ffab08c11a2ade0b5c1ecc36601

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried placing the UserInputService function in front, which didn’t work
    I did a lot of research on this and none of them seem to help

this is my current code for the block system, it is not my full code, ignore the camera shake


local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Tool = script.Parent
local AnimationsFolder = Tool:WaitForChild("Animations")

local UserInputService = game:GetService("UserInputService")


local ToolEquipped

local timeSinceLastHit = 0

local HoldingBlock = false

local Enable = true
local Check = 0 -- combo, left or right, 0 right/1 left

local Damage = 10
local Cooldown = 0.4

local PunchRightEvent = script.Parent.Combo.PunchRight
local PunchLeftEvent = script.Parent.Combo.PunchLeft
local KickLeftEvent = script.Parent.Combo.KickLeft
local PunchSpeed = 2

local SetValue = game:GetService("ReplicatedStorage"):WaitForChild("SetValue")

local HasStunnedAttribute = false

task.spawn(function()
	while task.wait(1) do
		if tick() - timeSinceLastHit >= 1 then --check if 3 seconds have elapsed since last hit
			Check = 0 --reset combo back to 0
		end
	end
end)






local function hasProperty(object, prop)
	local t = object[prop] --this is just done to check if the property existed, if it did nothing would happen, if it didn't an error will pop, the object[prop] is a different way of writing object.prop, (object.Transparency or object["Transparency"])
end





---------------------------------------------------====================
local camera = workspace.CurrentCamera

local CameraShakeEvent = Tool:WaitForChild("Events").CameraShake
local CameraShakeModule = require(game.ReplicatedStorage.CameraShaker)


local function ShakeCamera(shakeCf)
	-- shakeCf: CFrame value that represents the offset to apply for shake effect.
	-- Apply the effect:
	camera.CFrame = camera.CFrame * shakeCf
end

-- Create CameraShaker instance:
local renderPriority = Enum.RenderPriority.Camera.Value + 1
local camShake = CameraShakeModule.new(renderPriority, ShakeCamera)
camShake:Start()

CameraShakeEvent.OnClientEvent:Connect(function()
	camShake:ShakeOnce(2, 4, 0.2,0.3)
end)
------------------------===========================================









UserInputService.InputBegan:Connect(function(input, GameProcessed)
	if script.Parent.Parent:GetAttribute("Stunned") == true then print("found stun") return end
	if not GameProcessed and Humanoid.WalkSpeed < 25 then
		if Tool.Parent == Character then
			print("tool is in character")
			if input.KeyCode == Enum.KeyCode.F then
				print('is pressing f')
				HoldingBlock = true
				
				Humanoid.WalkSpeed = 8
				local BlockAnimationLoader = Humanoid:LoadAnimation(AnimationsFolder.Blocking)
				BlockAnimationLoader:Play()
				SetValue:FireServer("Block", true)
				
				repeat
					wait()
				until HoldingBlock == false
				BlockAnimationLoader:Stop()
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, GameProcessed)
	if not GameProcessed and Humanoid.WalkSpeed < 25 then
		if Tool.Parent == Character then
			if input.KeyCode == Enum.KeyCode.F then
				HoldingBlock = false
				
				Humanoid.WalkSpeed = 16
				SetValue:FireServer("Block", false)
			end
		elseif HoldingBlock == true then
			if input.KeyCode == Enum.KeyCode.F then
				HoldingBlock = false
				
				Humanoid.WalkSpeed = 16
				SetValue:FireServer("Block", false)
			end
			
		
		end
	end
end)

the script was also in a tool
Thank you for reading this, hope you can help me somehow

This may be a bit of a hacky solution, but you could parent the script to StarterCharacter, instead of StarterPlayer.

I already parented it to StarterCharacterScript, what would that change? edit: my bad I didn’t put it inside of StarterCharacterScript, but there is no way I can put this script inside of StarterCharacterScript because this script is inside a tool

Is this printing? If not do if Character:FindFirstChild(Tool.Name, false) then

I just did what you told me and this error appeared and it leads to the line where I load the animation, im not sure what this means, i never got it before

Oh wait, so it works BEFORE resetting and not after resetting?

Yes, it works before resetting

Either make sure that mouse isn’t nil shouldn’t have anything to do with this

Or load a new script everytime the tool is equipped and destroy the script everytime its unequipped OR

Unequip the tool when the person dies

AND make sure that you are updating the humanoid to the existing humanoid everytime the key is pressed the error could possibly be that Humanoid doesn’t exist anymore?

1 Like

Thank you so much this one helped me a lot and it works now! I added this line of code

Humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
1 Like

Not required but recommended…

Humanoid = script.Parent.Parent:FindFirstChild("Humanoid", false) or script.Parent.Parent:WaitForChild('Humanoid, 10)`

Just incase the humanoid just isnt there for some odd reason you have 10 extra seconds before the script breaks

1 Like