My sword doesn't do damage after the character resets, part doesn't become touched

Hi, I am trying to make a sword for myself and I think I have gotten most parts. My issue now is that the damage part on the sword doesn’t become “touched”, thus failing to FireServer, which controls the damage stuff. I am not very advanced in scripting so detailed replies would be much appreciated!!
Here is a clip of what I mean

This is the client script

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(player.Name)

local rp = game:GetService("ReplicatedStorage")

local sideValue = player.PlayerGui:WaitForChild("SideValue")
local rankValue = player:WaitForChild("Rank").Value

local activateSwordDmg = rp:WaitForChild("ActivateSwordDmg")

local ready, swinging = false, false

UIS.InputBegan:Connect(function(input, gameProcessed)
	local char = game.Workspace[player.Name]-- long conditions which just exit out can be made into a guard clause
	if ready or gameProcessed or input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	print("left clicked")

	if not char.RightHand:FindFirstChild('Weld') then
		print('no sword equipped')


	-- these flags tell the event when it can hit
	swinging = true
	ready = true
	local slashAnim ="Animation")
	slashAnim.AnimationId = ""
	local loadedAnim = char.Humanoid:LoadAnimation(slashAnim)

	swinging = false

	ready = false
local dmgpart = game.Workspace[player.Name]:WaitForChild("Sword"..rankValue):WaitForChild("DmgPart")

	if swinging and hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid"):IsA("Instance")then
		print("humanoid found")
		activateSwordDmg:FireServer(hit, sideValue.Value)
		swinging = false
		print("server fired")
	print("dmg resetted")
	dmgpart = game.Workspace[player.Name]:WaitForChild("Sword"..rankValue):WaitForChild("DmgPart")
	ready, swinging = false, false

The script didn’t even print “touched” so I am assuming that there is something wrong within the script itself. I also thought it was the fact that maybe dmgpart, ready, and swinging wasn’t in sync anymore when the player dies, so I added the function at the end to reset the values. But that didn’t work either. Now I am stuck and I don’t know what to do :confused:. Any help would be appreciated!

1 Like

Is this code running from the top when the player respawns, or only once?

This is because you didn’t add a equipped function where it would function if the tool was equipped, you are also missing a if statement where it would check if the character’s humanoid is gone from death like what @MysteriousVagabond mentioned about.

Also, why did you locate that script somewhere in startergui or starterpack? I could tell because you didn’t make up a variable of the parts since it would be messy except in the tool.

Now, you should put that script inside the tools, edit some variables (shortcuts for using terms like script.Parent.Part), make a if statement if the humanoid instance exists or nah, etc.

Or if you weren’t planning to do the scripts inside the tool, then just make an if statement and reset some if there are some parts that are supposed to happen when the player spawns.

It also runs when the player respawn. I get prints from the player clicking but not the part touching

So apparently, the dmgpart doesn’t touch anything after the player resets. Any ideas on how to fix this?

It’s because when the player dies, the dmgpart also disappears with the part, i’m not in the energy mood right now since it’s night and sorry if i didn’t read some of your posts that makes sense with that.

I suggest making an if statement where if the player’s health changes and it’s 0, you would then restart the script so it doesn’t continue after the player dies continuing the error, hope i helped.

1 Like

Thanks for your replies, as it turns out, all I had to do was to add player.CharacterAdded:Connect(function() above the variables. It is always these silly things that make me learn lol