Bug aquires when player dies

Hello, I really need help with this script and I would really appreciate it if you could try to help me. So basically I’m making an RPG game, and I’ve made a sword that you can equip and unequip when pressing “E” key, you can dash when pressing “Q” and etc…

Well, the problem with this is that when I die, sometimes none of the functions (For example equipping) don’t work anymore. So if I die, scripts can work, but sometimes it doesn’t work and I can fix it if I die multiple times and hope for it to work again.

This is how it looks in the explorer:
image

This is the code for “NewestCombat” local script:

local UIS = game:GetService("UserInputService")

local Remote = script:WaitForChild("Remote")

Remote:FireServer()

local camera = workspace.CurrentCamera
local DKeyDown = false
local SKeyDown = false
local AKeyDown = false
local WKeyDown = false

local CameraShaker = require(game:GetService('ReplicatedStorage'):WaitForChild("CameraShaker"));
local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
	camera.CFrame = camera.CFrame * shakeCf
end)

local Blocking = false

camShake:Start()

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
while Char.Parent == nil do
	Char.AncestryChanged:wait()
end
local HumRP = Char:WaitForChild("HumanoidRootPart")
local Hum = Char:WaitForChild("Humanoid")
local RollFrontAnim = Hum:LoadAnimation(script:WaitForChild("RollFront"))
local BackRollAnim = Hum:LoadAnimation(script:WaitForChild("BackRoll"))
local LeftRollAnim = Hum:LoadAnimation(script:WaitForChild("RightRoll"))
local RightRollAnim = Hum:LoadAnimation(script:WaitForChild("LeftRoll"))
local DashDebounce = false
local DashingDebounce = false

local CanDoAnything = true


UIS.InputBegan:Connect(function(Input,IsTyping)
	if IsTyping then return end
	
	if CanDoAnything == true then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 and Hum.Health > 0 then
			Remote:FireServer("Slash")	
			
		elseif Input.KeyCode == Enum.KeyCode.E and Hum.Health > 0 then
			Remote:FireServer("Equip")	
		elseif Input.KeyCode == Enum.KeyCode.Q then
			if DashDebounce == false and Char:FindFirstChild("Disabled") == nil then
				DashDebounce = true
				CanDoAnything = false
				delay(0.3,function()
					CanDoAnything = true
				end)
				delay(0.8,function()
					DashDebounce = false
				end)
				if WKeyDown then
					RollFrontAnim:Play()
					DashingDebounce = true
					
					delay(0.25,function()
						DashingDebounce = false
					end)
					
					repeat
						HumRP.Velocity = HumRP.CFrame.lookVector * 70
						wait(0.05)
					until DashingDebounce == false
				elseif SKeyDown then
					DashingDebounce = true
					
					BackRollAnim:Play()
					
					delay(0.25,function()
						DashingDebounce = false
					end)

					repeat
						HumRP.Velocity = HumRP.CFrame.lookVector * -70
						wait(0.05)
					until DashingDebounce == false
				elseif DKeyDown then
					DashingDebounce = true
					
					RightRollAnim:Play()
					
					delay(0.25,function()
						DashingDebounce = false
					end)

					repeat
						HumRP.Velocity = HumRP.CFrame.rightVector * 70
						wait(0.05)
					until DashingDebounce == false
				elseif AKeyDown then
					DashingDebounce = true
					
					LeftRollAnim:Play()
					
					delay(0.25,function()
						DashingDebounce = false
					end)

					repeat
						HumRP.Velocity = HumRP.CFrame.rightVector * -70
						wait(0.05)
					until DashingDebounce == false
				end	
			end	
			
		elseif Input.UserInputType == Enum.UserInputType.MouseButton2 then
			if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
				Remote:FireServer("Heavy")
			end
		end
	end	
end)

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local Player = game.Players.LocalPlayer
	local Char = Player.Character
	local Hum = Char:FindFirstChild("Humanoid")
	
	if UIS:IsKeyDown(Enum.KeyCode.F) and Hum.Health > 0 then
		if CanDoAnything == true then
			if Blocking == false then
				Blocking = true
				Remote:FireServer("Block",true)
			end	
		end	
	else
		if Blocking == true then
			Blocking = false
			Remote:FireServer("Block",false)
		end
	end
	
	if UIS:IsKeyDown(Enum.KeyCode.W) and Hum.Health > 0 then
		WKeyDown = true
	else
		WKeyDown = false
	end
	if UIS:IsKeyDown(Enum.KeyCode.A) and Hum.Health > 0 then
		AKeyDown = true
	else
		AKeyDown = false
	end
	if UIS:IsKeyDown(Enum.KeyCode.S) and Hum.Health > 0 then
		SKeyDown = true
	else 
		SKeyDown = false
	end
	if UIS:IsKeyDown(Enum.KeyCode.D) and Hum.Health > 0 then
		DKeyDown = true
	else 
		DKeyDown = false
	end
end)

Remote.OnClientEvent:Connect(function(Action)
	if Action == "HeavyShake" then
		camShake:Shake(CameraShaker.Presets.HeavyHit)
	else
		camShake:Shake(CameraShaker.Presets.SwordHit)
	end
end)

This is the code for “Combat” script:

local Remote = script.Parent

local Combo = 1
local DoingCombo = 0
local Debounce = false
local BlockingDebounce = false
local HeavyDebounce = false
local isBlocking = false
local Equipped = false
local EquipDebounce = false

local RS = game:GetService("ReplicatedStorage")
local RAYCAST_HITBOX = require(RS:WaitForChild("RaycastHitboxV3"))

local CanDoAnything = true

local TweenService = game:GetService("TweenService")

Remote.OnServerEvent:Connect(function(Player,Action,Bool)
	local Char = Player.Character
	local Hum = Char:WaitForChild("Humanoid")
	local HumRP = Char:WaitForChild("HumanoidRootPart")
	local RightArm = Char:WaitForChild("Right Arm")
	
	
	-- Animation Changer
	local ScriptAnimate = Char:WaitForChild("Animate")
	local Walk = ScriptAnimate:WaitForChild("walk")
	local WalkAnimation = Walk:WaitForChild("WalkAnim") 
	local Idle = ScriptAnimate:WaitForChild("idle")
	local IdleAnimation1 = Idle:WaitForChild("Animation1")
	local function ChangeAnimation()
		if WalkAnimation.AnimationId == "http://www.roblox.com/asset/?id=180426354" then
			WalkAnimation.AnimationId = "http://www.roblox.com/asset/?id=6975401582"
		elseif WalkAnimation.AnimationId == "http://www.roblox.com/asset/?id=6975401582" then
			WalkAnimation.AnimationId = "http://www.roblox.com/asset/?id=180426354"
		end
		if IdleAnimation1.AnimationId == "http://www.roblox.com/asset/?id=180435571" then
			IdleAnimation1.AnimationId = "http://www.roblox.com/asset/?id=6977070418"
		elseif IdleAnimation1.AnimationId == "http://www.roblox.com/asset/?id=6977070418" then
			IdleAnimation1.AnimationId = "http://www.roblox.com/asset/?id=180435571"
		end
	end
	
	
	if Hum.Jump == false then

		local FX = script:WaitForChild("FX")
		local SoundFolder = script:WaitForChild("Sounds")

		local StopAnims = function()
			local AnimationTracks = Hum:GetPlayingAnimationTracks()
			for i, track in pairs (AnimationTracks) do
				track:Stop()
			end
		end

		local StopEnemyAnims = function(Target)
			local AnimationTracks = Target:GetPlayingAnimationTracks()
			for i, track in pairs (AnimationTracks) do
				track:Stop()
			end
		end

		local HiteffectBall = function(Target,Pos)
			local ClonedBall = FX.Thing:Clone()
			ClonedBall.Parent = Target
			ClonedBall.CFrame = Pos
			ClonedBall.CFrame = CFrame.new(ClonedBall.Position, Target.Position)
			game.Debris:AddItem(ClonedBall,1)

			if DoingCombo == 4 then
				ClonedBall.BrickColor = BrickColor.new("Neon orange")
			end

			TweenService:Create(ClonedBall,TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{CFrame = ClonedBall.CFrame + ClonedBall.CFrame.lookVector * -7,Transparency = 1,Size = Vector3.new(0.087, 0.08, 3.35)}):Play()
		end
		
		
		local Sounds = function(Action2,Target)
			if Action2 == "HeavySound" then
				
			elseif Action2 == "NormalSound" then
				local NormalSound = SoundFolder.Normal:Clone()
				NormalSound.Parent = HumRP
				NormalSound.PlaybackSpeed = math.random(85,110)/100
				NormalSound:Play()
				game.Debris:AddItem(NormalSound,.4)				
				
			elseif Action2 == "BlockingSound" then	
				local BlockSound = SoundFolder.Block:Clone()
				BlockSound.Parent = Target
				BlockSound.PlaybackSpeed = math.random(90,110)/100
				BlockSound:Play()
				game.Debris:AddItem(BlockSound,.4)
				
			elseif Action2 == "Hit" then
				local HitSound = SoundFolder.Hit:Clone()
				HitSound.Parent = Target
				HitSound.PlaybackSpeed = math.random(90,125)/100
				HitSound:Play()
				game.Debris:AddItem(HitSound,1)
				
			elseif Action2 == "ClashingSound" then
				local ClashSound = SoundFolder.Clash:Clone()
				ClashSound.Parent = Target
				ClashSound.PlaybackSpeed = math.random(90,110)/100
				ClashSound:Play()
				game.Debris:AddItem(ClashSound,1)
				
			elseif Action2 == "HeavySound" then
				local HeavySound = SoundFolder.Heavy:Clone()
				HeavySound.Parent = HumRP
				HeavySound.PlaybackSpeed = math.random(45,65)/100
				HeavySound:Play()
				game.Debris:AddItem(HeavySound,.8)	
				
			elseif Action2 == "BlockBreak" then
				local BlockBreakSound = SoundFolder.BlockBreak:Clone()
				BlockBreakSound.Parent = Target
				BlockBreakSound:Play()
				game.Debris:AddItem(BlockBreakSound,.4)	
			elseif Action2 == "Sheathing" then
				local SheatheSound = SoundFolder.Sheathing:Clone()
				SheatheSound.Parent = Target
				SheatheSound:Play()
				game.Debris:AddItem(SheatheSound,1.8)	
			elseif Action2 == "UnSheathe" then
				local SheatheSound = SoundFolder.Unsheathe:Clone()
				SheatheSound.Parent = Target
				SheatheSound:Play()
				game.Debris:AddItem(SheatheSound,.7)	
			end
		end
		
		local ignoreList = {Char}
		
		local InsertDisabled = function(Target,Time)
			local Disabled = Instance.new("BoolValue",Target)
			Disabled.Name = "Disabled"
			game.Debris:AddItem(Disabled,Time)
		end	
		
		local BlockBreakAn = script:WaitForChild("Anims"):WaitForChild("BlockBreak")
		
		
		local Anims = script:WaitForChild("Anims")
		
		local Combo1Anim = Hum:LoadAnimation(Anims.Combo1)
		local Combo2Anim = Hum:LoadAnimation(Anims.Combo2)
		local Combo3Anim = Hum:LoadAnimation(Anims.Combo3)
		local Combo4Anim = Hum:LoadAnimation(Anims.Combo4)
		local HeavyAnim = Hum:LoadAnimation(Anims.Heavy)
		local BlockingAnim = Hum:LoadAnimation(Anims.Blocking)
		local UnSheatheAnim = Hum:LoadAnimation(Anims.Unsheathe)
		local SheatheAnim = Hum:LoadAnimation(Anims.Sheathe)
		
		
		if Action == "Slash" then
			if Debounce == false and Char:FindFirstChild("Disabled") == nil and CanDoAnything == true and Char:FindFirstChild("Blocking") == nil and Equipped == true then
				CanDoAnything = false
				Debounce = true
				delay(0.4,function()
					if DoingCombo == 4 then
						delay(0.4,function() -- delay for attacking again after combo
							Debounce = false
							CanDoAnything = true
						end)
					else
						CanDoAnything = true
						Debounce = false	
					end
				end)
				
				if Combo == 1 then
					Combo = 2
					DoingCombo = 1
					delay(1,function()
						if Combo == 2 then
							Combo = 1
						end
					end)
					
					Combo1Anim:Play()
					
				elseif Combo == 2 then
					Combo = 3
					DoingCombo = 2
					delay(1,function()
						if Combo == 3 then
							Combo = 1
						end
					end)
					
					Combo2Anim:Play()
					
				elseif Combo == 3 then
					Combo = 4
					DoingCombo = 3
					delay(1,function()
						if Combo == 4 then
							Combo = 1
						end
					end)
					
					Combo3Anim:Play()
					
				elseif Combo == 4 then 
					Combo = 1
					DoingCombo = 4
					
					delay(0.6,function()
						DoingCombo = 0
					end)
					
					Combo4Anim:Play()
				end
				
				Hum.JumpPower = 0
				
				delay(0.18,function()
					newHitbox:HitStart()
					
					Sounds("NormalSound",nil)
					
					local TrailCloned = FX.Trail:Clone()
					TrailCloned.Parent = Char.Katana.Blade
					TrailCloned.Attachment0 = Char.Katana.Blade.Attachment0
					TrailCloned.Attachment1 = Char.Katana.Blade.Attachment1
					game.Debris:AddItem(TrailCloned,0.2)
					
					delay(0.15,function()
						newHitbox:HitStop()
						Hum.JumpPower = 50
					end)
				end)	
			end
		elseif Action == "Equip" then
			if Char:FindFirstChild("Katana") and EquipDebounce == false and isBlocking == false and Char:FindFirstChild("Disabled") == nil and CanDoAnything == true then
				Equipped = false -- Unequip
				CanDoAnything = false
				ChangeAnimation()
				Hum.WalkSpeed = 0
				Hum.JumpPower = 0
				
				delay(0.4,function()
					Sounds("Sheathing",HumRP)	
				end)
				
				EquipDebounce = true
				delay(0.7,function()
					EquipDebounce = false
					local Sword = Char:FindFirstChild("Katana"):Destroy()
					CanDoAnything = true
					
					Hum.WalkSpeed = 16
					Hum.JumpPower = 50
				end)
				
				SheatheAnim:Play()
				
			elseif Char:FindFirstChild("Katana") == nil and EquipDebounce == false and isBlocking == false and Char:FindFirstChild("Disabled") == nil and CanDoAnything == true then
				CanDoAnything = false
				Equipped = true
				ChangeAnimation()
				EquipDebounce = true
				delay(0.5,function()
					EquipDebounce = false
				end)
				
				Sounds("UnSheathe",HumRP)
				
				local Sword = script:WaitForChild("Katana"):Clone()
				Sword.Parent = Char

				local Weld = Instance.new("Weld",RightArm)
				Weld.Part0 = RightArm
				Weld.Part1 = Sword.Handle
				Weld.C1 = CFrame.new(0.801995277, -0.00268554688, -1.8551712, -0.0109109292, 0.997559488, -0.0689636171, 0.99906683, 0.0137578184, 0.0409418531, 0.0417907275, -0.0684525371, -0.996778786)-- Equip
				
				UnSheatheAnim:Play()
				Hum.WalkSpeed = 0
				Hum.JumpPower = 0
				
				delay(0.4,function()
					Hum.WalkSpeed = 16
					Hum.JumpPower = 50
					CanDoAnything = true
				end)
				newHitbox = RAYCAST_HITBOX:Initialize(Sword.Blade,ignoreList)

				newHitbox.OnHit:Connect(function(hit, humanoid)
					if Char:FindFirstChild("Disabled") == nil then
						local EHumRP = humanoid.Parent:FindFirstChild("HumanoidRootPart")

						if Char:FindFirstChild("Heavy") then
							Remote:FireClient(Player,"HeavyShake")

							if humanoid.Parent:FindFirstChild("Blocking") then
								local Count = 0
								repeat
									Count = Count + 1
									HiteffectBall(HumRP,EHumRP.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
								until Count >= 5

								InsertDisabled(humanoid.Parent,.65)

								StopEnemyAnims(humanoid)

								local BlockVal = humanoid.Parent:FindFirstChild("Blocking")
								if BlockVal then
									BlockVal:Destroy()
								end

								local BlockBreakAnim = humanoid:LoadAnimation(BlockBreakAn)
								BlockBreakAnim:Play()

								Sounds("BlockBreak",EHumRP)

								humanoid.WalkSpeed = 0
								humanoid.JumpPower = 0

								delay(1.65,function() -- change this for time for unable to move when block breaked 
									humanoid.JumpPower = 50
									humanoid.WalkSpeed = 16
								end)
							else
								local Count = 0
								repeat
									Count = Count + 1
									HiteffectBall(EHumRP,EHumRP.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
								until Count >= 5

								Sounds("Hit",EHumRP)

								humanoid:TakeDamage(10)

								local Pos = HumRP.CFrame*CFrame.new(0,0,-15)

								local BP = Instance.new("BodyPosition",EHumRP)
								BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
								BP.D = 90
								BP.P = 400
								BP.Position = Pos.p
								game.Debris:AddItem(BP,0.4)

								humanoid.JumpPower = 0
								humanoid.WalkSpeed = 4
								delay(0.58,function()
									humanoid.JumpPower = 50
									humanoid.WalkSpeed = 16
								end)
							end
						else
							Remote:FireClient(Player,nil)

							if humanoid.Parent:FindFirstChild("Clashing") then

								Hum:TakeDamage(7)

								humanoid:TakeDamage(3)

								local Pos = HumRP.CFrame*CFrame.new(0,0,5)

								local BP = Instance.new("BodyPosition",HumRP)
								BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
								BP.D = 100
								BP.P = 300
								BP.Position = Pos.p
								game.Debris:AddItem(BP,0.3)

								humanoid.JumpPower = 0
								humanoid.WalkSpeed = 4
								delay(0.3,function()
									humanoid.JumpPower = 50
									humanoid.WalkSpeed = 16
								end)

								InsertDisabled(Char,0.5)

								Sounds("ClashingSound",EHumRP)

								local Count = 0
								repeat
									Count = Count + 1
									HiteffectBall(HumRP,EHumRP.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
								until Count >= 5

							elseif humanoid.Parent:FindFirstChild("Blocking") then
								humanoid:TakeDamage(1)
								Sounds("BlockingSound",EHumRP)

								local Count = 0
								repeat
									Count = Count + 1
									HiteffectBall(EHumRP,EHumRP.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
								until Count >= 5
							else	
								if DoingCombo == 1 then
									humanoid:TakeDamage(6)

									InsertDisabled(humanoid.Parent,0.6)

								elseif DoingCombo == 2 then
									humanoid:TakeDamage(6)

									InsertDisabled(humanoid.Parent,0.6)

								elseif DoingCombo == 3 then
									humanoid:TakeDamage(6)

									InsertDisabled(humanoid.Parent,0.6)
								elseif DoingCombo == 4 then
									humanoid:TakeDamage(17)

									local Pos = HumRP.CFrame*CFrame.new(0,0,-26)

									local BP = Instance.new("BodyPosition",EHumRP)
									BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
									BP.D = 90
									BP.P = 400
									BP.Position = Pos.p
									game.Debris:AddItem(BP,0.4)

									InsertDisabled(humanoid.Parent,1)
								end
								Sounds("Hit",EHumRP)
								humanoid.WalkSpeed = 4
								humanoid.JumpPower = 0
								delay(0.58,function()
									humanoid.JumpPower = 50
									humanoid.WalkSpeed = 16
								end)

								local Count = 0
								repeat
									Count = Count + 1
									HiteffectBall(EHumRP,EHumRP.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
								until Count >= 5

								local NewFX = FX.FXCut:Clone()
								NewFX.Parent = EHumRP
								NewFX:Emit(1)
								game.Debris:AddItem(NewFX,0.6)

							end					
						end
					end	
				end)
			end
			-- Made by Garli
		elseif Action == "Block" then
			if Bool == false and isBlocking == true then
				BlockingDebounce = true
				delay(.09,function() -- change this for blocking delay
					BlockingDebounce = false
				end)
				
				isBlocking = false
				
				delay(0.09,function() -- change this for blocking delay
					CanDoAnything = true
				end)
				
				local AnimationTracks = Hum:GetPlayingAnimationTracks()
				for i, track in pairs (AnimationTracks) do
					if track.Name == "Blocking" then
						track:Stop()
					end
				end
			
				Hum.JumpPower = 50
				Hum.WalkSpeed = 16
					
				local Value = Char:FindFirstChild("Blocking")
				if Value then
					Value:Destroy()
				end
			elseif Char:FindFirstChild("Disabled") == nil and CanDoAnything == true and Bool == true and BlockingDebounce == false and isBlocking == false and Equipped == true then
				isBlocking = true
				CanDoAnything = false
				
				Hum.JumpPower = 0
				
				BlockingAnim:Play()
				
				local Val = Instance.new("BoolValue",Char)
				Val.Name = "Clashing"
				game.Debris:AddItem(Val,0.15)
					
				Hum.WalkSpeed = 5
					
				local Value = Instance.new("BoolValue",Char)
				Value.Name = "Blocking"
			end	
		elseif Action == "Heavy" then
			if HeavyDebounce == false and Char:FindFirstChild("Disabled") == nil and CanDoAnything == true and Char:FindFirstChild("Blocking") == nil and Equipped == true then
				CanDoAnything = false
				HeavyDebounce = true
				
				delay(0.8,function()
					HeavyDebounce = false
				end)
				delay(0.5,function()
					CanDoAnything = true
				end)
				
				HeavyAnim:Play()
				
				Hum.JumpPower = 0
				Hum.WalkSpeed = 8
				
				delay(0.5,function()
					newHitbox:HitStart()
					
					Sounds("HeavySound",nil)
					
					local TrailCloned = FX.Trail:Clone()
					TrailCloned.Parent = Char.Katana.Blade
					TrailCloned.Attachment0 = Char.Katana.Blade.Attachment0
					TrailCloned.Attachment1 = Char.Katana.Blade.Attachment1
					game.Debris:AddItem(TrailCloned,0.2)
					
					local Val = Instance.new("BoolValue",Char)
					Val.Name = "Heavy"
					game.Debris:AddItem(Val,.19)

					delay(0.19,function()
						newHitbox:HitStop()
						Hum.WalkSpeed = 16
						Hum.JumpPower = 50
					end)
				end)
			end	
		end
	end
end)

Is there a way maybe that I reset the scripts when player respawns or something like that? How do you think the best way is it to fix it?

Put the player.CharacterAdded event in your scripts, this fires whenever the character loads (when it joins, resets or dies).

CharacterAdded doesn’t fire when they die, it fires after they respawn.

Already tried that, but that event doesn’t fire at all for some reason, but it works with event when player dies and I’ve tried multiple things to fix it like that and nothing worked…

I had the same problem. I think the solution is to put it into StartedPlayer → StarterCharacterScripts. I mean it fixed it for me. Here’s my post: Punching Script Breaks on Death - #6 by k_as