Touched event not detecting the "enemy"

Hello, basically I wanted to make a beam script that whenever a player or dummy touches it, it receives the damage, but it doesn’t detect the dummy or another player, just the player who used the “beam”

I tried using a filter so it doesn’t damage the player who used the beam, but it doesn’t detect the enemy or dummy

Here is the touched is the touched event

		local hit = false
		local connection = shockwave.Touched:Connect(function(hitPart)
			if not hit then
				if hitPart.Parent ~= player.Character or weapon then
					print("Wrong Hit")
				else
					local Enemyhumanoid = hitPart.Parent:FindFirstChild("Humanoid")
					if Enemyhumanoid then
						local cframe = hitPart.Parent.Torso.CFrame
						hit = true
						Enemyhumanoid:TakeDamage(45)
						
						end
				end
			end
		end)
		if hit then
			connection:Disconnect()
			break
		end

is there a mistake with the script?

1 Like

If the object does not have a humanoid but it also not the character or the weapon then it would error. That’s probably your problem.

I don’t think so, if the object doesn’t have a humanoid it would ignore since hit is still false, player equals to the one who fired the beam, the weapon is from the player who used it, so it wouldn’t be a problem, since is a different character.

Could you take a picture of the enemy in the explorer(opened up). I have to go now but will be able to help later. Sorry I wasn’t able to help you.

The enemy is a regular dummy or another player, I don’t think it’s necessary to send a screenshot of the dummy, but I’ll still send it since you asked for it
Screenshot_637

Can you print out what exactly is hitPart? That may help significantly.

Also note that it may be hitting, but if this is a client-sided script, you cannot apply damage to them directly. You have to tell the server to do the damage.

Where is this script placed? Inside a part? inside a tool? in ServerScriptService?
How are u getting the player.Character and the weapon? In order to filter them?

Btw, the first thing I notice is some arguments missing maybe?
if hitPart.Parent ~= player.Character or hitPart.Parent ~= weapon then

the weapon variable what it is? a bool? the weapon itself? I dont understand clearly what ur beam is. And how the player is using it? a click? its a tool?

1 Like

its a remote event, the problem is that the script is not detecting the “enemy” aka dummy, Wrong Hit means the player character who used the beam or the weapon(BParts)
https://gyazo.com/f1032553bea784b29f92ca4608f2bd3e

ServerScriptService, its a remote so basically I did it this:
remote.OnServerEvent:Connect(function(player)
local weapon = player.Character.Excalibur
end
and the weapon is the weapon itself, i can send the full script if you guys want

Yeah, would be good to see the script. But you said something confusing. You said “Wrong hit” print is when the owner of the weapon is being touched by the beam?? Actually is the opposite.

if hitPart.Parent ~= player.Character or hitPart.Parent ~= weapon then
     print("This is what runs when the Hit part is NOT the owner of the weapon")
else
     print("This is what runs when the touched part IS the owner of the weapon")
end

If the variable player, contains the client that used a gui button or something to fire the remote. Thats the owner of the weapon, who is gonna use it. Then u are comparing

if the hit part is different than the owner of the weapon then... "Do the damage!!"… Not print (Wrong Hit…)

You were right, but it still only detect my character and my weapon for some reason, basically it damage me when I try to use the move
https://gyazo.com/37dc3587e113279fc02e7c0608dcd99b

Show your script. Cause I used ur script, created a remote and created a GUI button. I just edited a little your script, and its working fine for me. It only damage the other players except the one who pressed the button (the owner)

local hit = false
local shockwave = game.Workspace.Weap

local RepS = game:GetService("ReplicatedStorage")
local remote = RepS:WaitForChild("Test")

local client
--local weapon 
	
remote.OnServerEvent:Connect(function(player)
	--weapon = player.Character.Excalibur
	client = player
end)

local connection = shockwave.Touched:Connect(function(hitPart)
	if not hit then
		if hitPart.Parent ~= client.Character then
			local Enemyhumanoid = hitPart.Parent:FindFirstChild("Humanoid")
			if Enemyhumanoid then
				--local cframe = hitPart.Parent.Torso.CFrame
				hit = true
				Enemyhumanoid:TakeDamage(45)
			end
		else
			print("Wrong Hit")
		end
	end
end)

if hit then
	connection:Disconnect()
	--break
end
1 Like

what I mean is, the script is ignoring the operator and not detecting the dummy

This is the full script:

local remote = game.ReplicatedStorage.Remotes.ExcaliburBeam
local tweenservice = game:GetService("TweenService")
local assets = game.ReplicatedStorage.Assets
remote.OnServerEvent:Connect(function(player)
	local maning = true
	local outsphere = player.Character.ExcaliburMana1
	local insphere = player.Character.ExcaliburMana2
	local hit = false
	local weapon = player.Character.Excalibur
	
	local cylinder = assets.Meshes.OutEXBeam:Clone()
	cylinder.CFrame = outsphere.CFrame * CFrame.new(1, 0, -cylinder.Size.X / 2)
	cylinder.Parent = workspace
	local cylinder2 = assets.Meshes.InEXBeam:Clone()
	cylinder2.CFrame = cylinder.CFrame
	cylinder2.Parent = workspace
	
	local shockwave = assets.Meshes.ExPropBeam:Clone()
	shockwave.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder.Size.X)
	shockwave.Parent = workspace

	for i = 1, 95 do
		cylinder.Size = cylinder.Size + Vector3.new(5, 0, 0)
		cylinder.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder.Size.X / 2) * CFrame.Angles(0, math.rad(90), 0)
		cylinder2.Size = cylinder2.Size + Vector3.new(5, 0, 0)
		cylinder2.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder2.Size.X / 2) * CFrame.Angles(0, math.rad(90), 0)
		shockwave.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder.Size.X)
		wait()
		local hit = false
		local cola = shockwave.Touched:Connect(function(hitPart)
			if not hit then
				if hitPart.Parent ~= player.Character or hitPart.Parent ~= weapon then
					print("not touched by the owner of the weapon")	
					print(hitPart.Name)
					local Enemyhumanoid = hitPart.Parent:FindFirstChild("Humanoid")
					if Enemyhumanoid then
						local cframe = hitPart.Parent.Torso.CFrame
						hit = true
						Enemyhumanoid :TakeDamage(45)
					end
				else
					print("Touched by the owner of the weapon")	
				end
			end
		end)
		if hit then
			cola:Disconnect()
			break
		end
		wait()
	
	end
	
	wait(8)
	local ani1= {
	}
	local ani2 = {
	}
	cylinder:Destroy()
	cylinder2:Destroy()
	shockwave:Destroy()
end) ```

Idk if its related with the problem but. You are connecting 95 times the same touchEvent.

On that for i = 1, 95 do You are connecting the function to the event shockwave.Touched:Connect(function) 95 times. Thats not good. You only need to connect it once, its only 1 mesh, not 95 clones
local shockwave = assets.Meshes.ExPropBeam:Clone()

1 Like

I don’t think so, tried what you said and the script is still ignoring the operator, so I don’t really know why does it damage me or why it doesn’t detect other players
Screenshot_642

Double check your code. Cause I edited it and its working, look the video, My character gets not damage and the enemy receives the damage nicely.

robloxapp-20210204-2145453.wmv (3.3 MB)

local remote = game.ReplicatedStorage.Remotes.ExcaliburBeam
local tweenservice = game:GetService("TweenService")
local assets = game.ReplicatedStorage.Assets

remote.OnServerEvent:Connect(function(player)
	local maning = true
	local outsphere = player.Character.ExcaliburMana1
	local insphere = player.Character.ExcaliburMana2
	local hit = false
	local weapon = player.Character.Excalibur

	local cylinder = assets.Meshes.OutEXBeam:Clone()
	cylinder.CFrame = outsphere.CFrame * CFrame.new(1, 0, -cylinder.Size.X / 2)
	cylinder.Parent = workspace
	local cylinder2 = assets.Meshes.InEXBeam:Clone()
	cylinder2.CFrame = cylinder.CFrame
	cylinder2.Parent = workspace

	local shockwave = assets.Meshes.ExPropBeam:Clone()
	shockwave.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder.Size.X)
	shockwave.Parent = workspace
	
	local hit = false
	local cola = shockwave.Touched:Connect(function(hitPart)
		if not hit then
			if hitPart.Parent ~= player.Character or hitPart.Parent ~= weapon then
				print("not touched by the owner of the weapon")	
				print(hitPart.Name)
				local Enemyhumanoid = hitPart.Parent:FindFirstChild("Humanoid")
				if Enemyhumanoid then
					--local cframe = hitPart.Parent.Torso.CFrame
					hit = true
					Enemyhumanoid:TakeDamage(45)
				end
			else
				print("Touched by the owner of the weapon")	
			end
		end
	end)
	if hit then
		cola:Disconnect()
		--break
	end
	
	
	for i = 1, 95 do
		cylinder.Size = cylinder.Size + Vector3.new(5, 0, 0)
		cylinder.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder.Size.X / 2) * CFrame.Angles(0, math.rad(90), 0)
		cylinder2.Size = cylinder2.Size + Vector3.new(5, 0, 0)
		cylinder2.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder2.Size.X / 2) * CFrame.Angles(0, math.rad(90), 0)
		shockwave.CFrame = outsphere.CFrame * CFrame.new(0, 0, -cylinder.Size.X)
		wait()
	end

	wait(8)
	local ani1= {
	}
	local ani2 = {
	}
	cylinder:Destroy()
	cylinder2:Destroy()
	shockwave:Destroy()
end)

btw. I disabled the checking torso CFrame, cause it gaves me an error. I should check why, but Im a little busy now. But, works fine for me, after many tests, I never receive damage on my character, and only the other player takes the damage.
I agree with @SyntaxWizard too, would be better if you split those statements. But, still working without the split. Paste ur code again to check whats the problem, compare it with this one and find the issue. Its working fine. I would suggest a different approach but the code seems fine.

1 Like

I’m 99.99% sure it has something to do with a single line operator checking.
I’ve ran into this problem but with another scenario.

If you have this script here:

local part = script.Parent
local a = 1

part.Touched:Connect(function(hit)
	if a ~= 1 or a ~= 2 then
		print("A")
	end
end)

At first glance, it shouldn’t print A. But upon running it, setting a to 1 or 3 will still run the code inside.

You’ll have to split those up into 2 different conditional checking.

1 Like

alr, basically I added IsDescendantOf(player.Character) and changed the “or” statement to “and” and now it doesn’t damage me, but is not detecting the dummy, basically the same problem as the start

	local cola = shockwave.Touched:Connect(function(hitPart)
		if not hit then
			if hitPart.Parent ~= player.Character and hitPart.Parent ~= weapon then
				if hitPart:IsDescendantOf(player.Character) then
					print("d of player.Character")	
				else 
					print("not touched by the owner of the weapon")	
					print(hitPart.Name)
					print(hitPart.Parent.Name)
					local humanoid = hitPart.Parent:FindFirstChild("Humanoid")
					if humanoid then
						local cframe = hitPart.Parent.Torso.CFrame
						hit = true
						humanoid:TakeDamage(45)
					end	
				end
				
			else
			--[[	print("Touched by the owner of the weapon")	
				print(hitPart.Name)
				print(hitPart.Parent.Name) ]]
			end
		end
	end) ```

Tried this and it worked but I fixed it with other way, now its not detecting the dummy