Script completely breaks when running while dying

Hello!

So I ran into an issue. I made a block system. But everytime I died while blocking the script just completely breaks and I can’t block anymore.

Note: This script is a local script.


Here’s the script :


local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")

----------------------------------------------------

-- Variables

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local PlayerScripts = Player:WaitForChild("PlayerScripts")

local isUsingBlock = Player:WaitForChild("Weapon"):WaitForChild("WeaponAttacks"):WaitForChild("IsUsingAttack5")

local Animation = Instance.new("Animation", Character)
Animation.Name = "BlockAnimation"
Animation.AnimationId = "rbxassetid://8993539850"
local Animation_Loader = Humanoid:LoadAnimation(Animation)

----------------------------------------------------

-- Values

local Key = "T"
local Debounce = false
local Duration = 3
local DebounceTime = 0.25

----------------------------------------------------

-- Code

for i, tools in pairs(Player.Backpack:GetChildren()) do
	if tools:FindFirstChild("IsAnItem") == nil then
		UIS.InputBegan:Connect(function(input, typing)
			if typing then return end

			if input.KeyCode == Enum.KeyCode[Key] and Debounce == false then
				Debounce = true

				local ToolCodes = tools:FindFirstChild("Main_Attack")
				local DashCode = PlayerScripts:WaitForChild("Dash")
				local BlockGUI = RS:WaitForChild("BlockGUI"):Clone()

				Animation_Loader:Play()

				Humanoid.WalkSpeed = 5
				Humanoid.JumpPower = 0

				Character:FindFirstChild("IsBlocking").Value = true

				ToolCodes.Disabled = true
				DashCode.Disabled = true
				BlockGUI.Parent = HumanoidRootPart

				wait(Duration)

				Humanoid.WalkSpeed = RS:WaitForChild("CurrentWalkSpeed").Value + Player:WaitForChild("Attributes"):WaitForChild("Speed").Value
				Humanoid.JumpPower = 60

				ToolCodes.Disabled = false
				DashCode.Disabled = false
				Animation_Loader:Stop()
				BlockGUI:Destroy()

				Character:FindFirstChild("IsBlocking").Value = false
				isUsingBlock.Value = false

				wait(DebounceTime)

				Debounce = false
			end
		end)
	end
end

Is there a way I can fix this? Thanks!

Did you put it into StarterPlayerScripts? If so, try putting it into StarterCharacterScripts instead.

1 Like

Try moving the InputBegan Event outside of the loops and If statement so the event would be able to listen to Inputs being made by the user. You can then add the loops and If statement Inside the event to do a check.

1 Like

Yes it is in StarterPlayerScripts

I just moved it to StarterCharacterScripts, let’s see if it works.

Seems like it’s working now. Thanks!

No problem, but there’s a small issue. The way you wrote the script… isn’t the best. Let’s say you have 3 tools, that would be 3 new connections every respawn to UserInputService that never go away.

1 Like

How can I optimize it?

char limit