After Player Dies They Cant Use Sword

After a player dies they cant swing their sword. I have no idea why, and this issue recently started around a month ago when I had the swords for around a year.

 local RMod = require(game.ReplicatedStorage.RegionModule)
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local c = Player.Character
local mouse = Player:GetMouse()

local Enabled = true

game.ReplicatedStorage.Sword:FireServer("SetUp")

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,typing)
	if typing then return end
	if c:findFirstChild("Sword") and Enabled == true then
	if input.KeyCode == Enum.KeyCode.R then
		game.ReplicatedStorage.Sword:FireServer("Skill1")
	elseif input.KeyCode == Enum.KeyCode.F then
		game.ReplicatedStorage.Sword:FireServer("Skill2")
	elseif input.KeyCode == Enum.KeyCode.V then
			game.ReplicatedStorage.Sword:FireServer("Skill3")
		end
	end
end)

local Combo = 0

mouse.Button1Down:connect(function() 
	if c:findFirstChild("Sword") and Enabled == true then
		Enabled = false
		if Combo == 0 then
		   Combo = 1
		   local Track = Instance.new("Animation")
			Track.AnimationId = "rbxassetid://6709053351"
		   local Anim = c.Humanoid:LoadAnimation(Track)
			Anim:Play()
			local move = Instance.new("BodyPosition")
			move.MaxForce = Vector3.new(1,0,1) * 5000
			move.Parent = c.HumanoidRootPart
			move.Position = c.HumanoidRootPart.CFrame*Vector3.new(0,0,-25)
			wait(.2)
			move:Destroy()
		elseif Combo == 1 then
			Combo = 2
			local Track = Instance.new("Animation")
			Track.AnimationId = "rbxassetid://6709060521"
			local Anim = c.Humanoid:LoadAnimation(Track)
			Anim:Play()
			local move = Instance.new("BodyPosition")
			move.MaxForce = Vector3.new(1,0,1) * 5000
			move.Parent = c.HumanoidRootPart
			move.Position = c.HumanoidRootPart.CFrame*Vector3.new(0,0,-25)
			wait(.2)
			move:Destroy()
		elseif Combo == 2 then
			Combo = 3
			local Track = Instance.new("Animation")
			Track.AnimationId = "rbxassetid://6709042277"
			local Anim = c.Humanoid:LoadAnimation(Track)
			Anim:Play()
			local move = Instance.new("BodyPosition")
			move.MaxForce = Vector3.new(1,0,1) * 5000
			move.Parent = c.HumanoidRootPart
			move.Position = c.HumanoidRootPart.CFrame*Vector3.new(0,0,-25)
			wait(.2)
			move:Destroy()
		else 
			Combo = 0 
			wait()
			local Track = Instance.new("Animation")
			Track.AnimationId = "rbxassetid://6709037008"
			local Anim = c.Humanoid:LoadAnimation(Track)
			Anim:Play()
			local move = Instance.new("BodyPosition")
			move.MaxForce = Vector3.new(1,0,1) * 5000
			move.Parent = c.HumanoidRootPart
			move.Position = c.HumanoidRootPart.CFrame*Vector3.new(0,0,-25)
			wait(.2)
			move:Destroy()
		end
		local HB = Instance.new("Part")
		local Pos = c.HumanoidRootPart.CFrame*CFrame.new(0,0,-4).p
		local Reg = Region3.new(Pos-Vector3.new(2,2,2),Pos+Vector3.new(2,2,2))
		HB.Size = Vector3.new(4,2,8)
		HB.CFrame = c.HumanoidRootPart.CFrame*CFrame.new(0,0,-4)
		local AR = RMod.FromPart(HB)
		local RTable = AR.Cast(AR,c)
		for i,v in pairs(RTable) do
			if v.Name == "HumanoidRootPart" or v.Name == "LeftUpperLeg" or v.Name == "LeftLowerLeg" or v.Name == "RightLowerLeg" or v.Name == "RightUpperLeg" or v.Name == "UpperTorso" and v.Parent:findFirstChild("Humanoid") then
				local RTable = workspace:FindPartsInRegion3(Reg, c, 5000)
				for i,v in pairs(RTable)do
					if v.Name == "HumanoidRootPart"  or v.Name == "LeftUpperLeg" or v.Name == "LeftLowerLeg" or v.Name == "RightLowerLeg" or v.Name == "RightUpperLeg" or v.Name == "UpperTorso" and v.Parent:FindFirstChild("Humanoid")then
						game.ReplicatedStorage.Sword:FireServer("Slash", v.Parent)
					end
				end
			end
		end
		wait(.1)
		Enabled = true
		wait(.1)
	end
end)

Likely an issue with the character not being in the Player instance; check console.

Ive checked it and nothing appeared, no warning or error.

Try reinitializing the ‘c’ (Character) variable with an Equipped function.

Sorry but it didnt work ;(, is there anything else you can think of?

What do you think might be causing the issue?

When you die, the sword will disappear from the inventory and the sword wont work. If you die again and spam switch swords if wont disappear from the inventory, but if you do the exact same thing again it wont work. So everything i have tried that has worked, doesnt work once i do the exact thing again.

The tool gets destroyed on death?
If so, put it into StarterGear or StarterPack for testing.

No it doesnt destroy on death im trying something right now

Any errors? Where are you storing this script? Is the script being added to the player every time they spawn or do they always have it?

the script is inside the sword, and no errors or warnings.

1 Like

Can you add prints throughout the script and replicate the error again. See where the prints stop.

Its best to put a unique print under each loop or if statement and anything thay can yield (like :WaitForChild). By “unique” print i just mean dont put “print(“hi”)” for every line cause it will be hard to tell which print is for what.

1 Like

very smart idea, so line 31 where it says

if c:findFirstChild("Sword") and Enabled == true then

is the issue

Now right before that line print(c:findFirstChild(“Sword”)) and print(Enabled). See which (or both) conditions aren’t being met.

it says on the line for the c:findfirstchild(“Sword”) nil which i think means that it doesnt detect the sword. The enabled is true which means that is working as intended.

Can you print(c ). It might be that when the character dies/respawns and you try to use the c variable. It returns nil because its referencing the old character that died and no longer exist. This only applies if the script isn’t being runned every time the player spawns (which is what you said).

If this is the case, the simple solution would be to define the character inside the functions/InputConnection.

Alright ill try it now, hopefully it works

I just tried it and it detects my name still and doesnt return nil.

Oh you might still be able to reference the character even if its deleted. So actually print(c.Parent). If its workspace then its an issue with the sword, if its nil then its the issue i stated above

You were right c.Parent returns nil. How can I fix this if you dont mind me asking?