Why does my touched event not work after player dies?

More information it works if you go near it

What happens is when the enemy respawns the touched event’s parameter hit doesn’t pick up the enemy character

1 Like

Can we see some code? No one will be able to help you if you don’t show any code.

2 Likes

its a bit lengthy, when the enemy player respawns I can’t hit it, but the touched event is still running

local function takeDamage(part, player)
		
	local char = player.Character or player.CharacterAdded:Wait()
	
	part.Touched:Connect(function(hit)
		print(hit)
		local damage
		if hit.Parent ~= char then 

				local humanoid = hit.Parent:FindFirstChild("Humanoid")
				if humanoid then 
					if touchDebounce then
						touchDebounce = false 
						
						if part.Shape == Enum.PartType.Ball then 
							damage = 10
						else if part.Shape == Enum.PartType.Cylinder then 
								damage = 20
							else if part.Shape == Enum.PartType.Block then 
									damage = 30
								end
							end
						end 
						
						humanoid:TakeDamage(damage)	
						
						local enemyChar = humanoid.Parent 
						local enemyHMR = enemyChar.HumanoidRootPart
						local enemyCFrame = enemyHMR.CFrame
						
						rEvent4:FireClient(player, damage, enemyCFrame)
						
						delay(0.5, function()
							touchDebounce = true
							
						end)
							
						
						humanoid.Died:Connect(function()
							
							local enemyChar=  humanoid.Parent
							local cloneFolder = enemyChar:FindFirstChild("cloneFolder")
							
							
							
							rEvent3:FireAllClients(enemyChar, char)
		
							
							for _,v in pairs(starterPack:GetChildren()) do 
								if v.Name ~= "Tool" then 
									v:Destroy()
								end
							end
							
							if cloneFolder then 
								for _,v in pairs(cloneFolder:GetChildren()) do 
									local Player = Players:GetPlayerFromCharacter(enemyChar)
									local userId = Player.UserId
									partsCounter[userId] = {}
									
									v:Destroy()
								end
								cloneFolder:Destroy()
							end
							
							 
							for _,v in pairs(enemyChar:GetDescendants()) do 
								
								if v:IsA("Motor6D") then 
									if v.Name ~= "Neck" then
										local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
										a0.CFrame = v.C0 
										a1.CFrame = v.C1
										a0.Parent = v.Part0 
										a1.Parent = v.Part1 
										
										local b = Instance.new("BallSocketConstraint")
										b.Attachment0 = a0 
										b.Attachment1 = a1 
										b.Parent = v.Part0 
										v.Part0 = nil 
										v.Part1 = nil 
									end
								end		
							end
						end)	
					end	
				end	
		end	
		--part:Destroy()	
	end)
end

Instead of setting the character and checking that, simply get the character and check if it belongs to that player.

if hit.Parent:FindFirstChild('Humanoid') then
    local hitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hitPlayer == player then
        -- do stuff
    end
end

I refactored some of the code, I don’t fully know what you’re doing so you’ll have to fill in some of the blanks.

CODE

local function takeDamage(part, player) -- you should pass in a character variable with a remote event
	
	local character = player.Character or player.CharacterAdded:Wait()
	
	local touched = nil
	touched = part.Touched:Connect(function(hit)
		local damage = nil
		local player = nil
		
		if hit and hit.Parent:FindFirstChild("Humanoid") and not hit:IsDescendantOf(character) then
			touched:Disconnect() -- stop firing the touched event (So you don't have to use a debounce), you can place this wherever you wish.
			local target = hit.Parent
			if game.Players:GetPlayerFromCharacter(target) then -- replace game.Players with a "Players" variable grabbed with :GetService()
				player = game.Players:GetPlayerFromCharacter(target)
			else
				player = nil
			end
			
			if part.Shape == Enum.PartType.Ball then
				damage = 10
			elseif part.Shape == Enum.PartType.Cylinder then
				damage = 20
			else
				damage = 30
			end
			
			target.Humanoid:TakeDamage(damage)
			if player then
				-- an event fires with the player as an argument / other arguments you need
				someEvent:FireClient(player, damage, target.HumanoidRootPart.CFrame) -- remember we checked if this was a player so we can pass in target
			end
			
		end
		
	end)
	
end

I fixed the problem but that creates another problem, I’m tweening this and i read on another place that touched events don’t work on anchored parts but if I don’t anchor the person who recieves the hit will see the part glitch a bit

in the video the projectile goes up and down a bit

Can you try setting the parts NetworkOwner to the client?

-- put this code after everything is ready, the tween, velocity, etc...

part:SetNetworkOwner(nil) -- replace part with whatever part is being used.

I already set this in the code above do I need to make it nil?

Yeah, I’m pretty sure it has to be nil, read some articles about NetworkOwnership to help you.

1 Like

ok nice thanks I fixed it i just deleted the setownershipcode to player code

Does the networkownership reset when the player dies?