Why isn't this system working?

Hey, I’m Expistic.

Currently struggling to figure out what is going on with the following script.

Here’s how it’s supposed to work:

if the left arm of the player is touched, then it searches for a humanoid. if theres no humanoid, then it doesnt continue the touched function. if there IS a humanoid, then it loops thru the players. if the touched object’s name is found in the loop of player names, then it recognizes the player, creates a variable for it(plr2), and checks for a couple values in that player. if they’re true, then it uses DMG1. if its false, but there’s still a player, then it’s supposed to only use DMG2. However, it uses DMG2 AND DMG3. DMG3 is reserved for models that aren’t players.

my code:

player.Character["Left Arm"].Touched:Connect(function(hit)
	if player.punch_CD.Value == false then
		player.punch_CD.Value = true
			if hit.Parent:FindFirstChild("Humanoid") then
				for i,v in pairs(game:GetService("Players"):GetChildren()) do
					if hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
						hit.Parent.HumanoidRootPart.BodyVelocity:Destroy()
					end
							
				if v.Name == hit.Parent.Name then
								
					local plr2 = game.Players:GetPlayerFromCharacter(hit.Parent)
								
					if plr2.MainStats.formType.Value == "UI" and plr2.Stats.inForm.Value == true then
						local dmg1 = "Dodged!"
						print("DMG1")
						game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg1)
					elseif plr2.Stats.inForm.Value == false then
						print("DMG2")
						local dmg2 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
						game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg2)
					end
								
				elseif v.Name ~= hit.Parent.Name then
								
					local dmg3 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
					hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dmg3
					game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg3)
					print("DMG3")
								
				end
			end
		end
	end
end)
1 Like

Have you considered 2 parts hitting at the same time? Might be worth printing the “hit” part to see what is calling this function and how many times. These if statements are properly nested.

1 Like

I’ll give that a try, I’ll respond with an image

1 Like

image

I printed out what it was hitting. This was the result. The hit player’s HumanoidRootPart.

1 Like

It’s really odd because of the following line:

elseif v.Name ~= hit.Parent.Name then

I’m trying to make it so that if the hit model isn’t a player, then it does DMG3.

1 Like

I think I got it.

  1. You are using a for loop for all players. (one is hit, one is hitting, I presume)
  2. One of those players will match, because he was hit (DMG2)
  3. One will not? (DMG3)
1 Like

That’s clever, but I printed out hit.Parent.Name this time, and DMG2 and DMG3 printed out the same model name

Little peak at the output here:
image

2 Likes

Try printing out v.Name, I think thats the problem


aHA! So you were right, it’s detecting Player1. But now the question is: why?

1 Like

I’m gonna try smt rq, and I’ll lyk if it works or not.

This means the same part is being checked twice, which means it’s the for loop, not the if loop. What I think you WANT to do for DMG3 is check if none of the players match.

Yep yep, that’s exactly what I need to do.

1 Like

How would I go about doing that, also?

1 Like

Haha, I thought you had it. OK, here’s what you need to do:

  1. move “DMG3” out of the for loop.
  2. define a test boolean before the for loop and set it to false
  3. in the for loop:
    a) compare each v.name to the parent like you do, then
    b) test = test or compare

If the test boolean is still false at the end, you did not find a match, and that should trigger DMG3

This needs to be tested and debugged, because I’m a rookie… but something like this.

player.Character["Left Arm"].Touched:Connect(function(hit)
	if player.punch_CD.Value == false then
		player.punch_CD.Value = true
		if hit.Parent:FindFirstChild("Humanoid") then
			local hitTest = false
			for i,v in pairs(game:GetService("Players"):GetChildren()) do
				if hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
					hit.Parent.HumanoidRootPart.BodyVelocity:Destroy()
				end

				if v.Name == hit.Parent.Name then
					hitTest = true
					local plr2 = game.Players:GetPlayerFromCharacter(hit.Parent)

					if plr2.MainStats.formType.Value == "UI" and plr2.Stats.inForm.Value == true then
						local dmg1 = "Dodged!"
						print("DMG1")
						game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg1)
					elseif plr2.Stats.inForm.Value == false then
						print("DMG2")
						local dmg2 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
						game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg2)
					end
				end
				
			end
		if not hitTest then

			local dmg3 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dmg3
			game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg3)
			print("DMG3")			
		end
	end
end)
1 Like

What type of loop would I use to compare all the players? how would it be written?

sorry, please bear with me, haha, it’s 2 am

You have all the code, and the loop. You put the second test in the for loop, and use the result after you are done.

OH CRIPES…
that’s right haha, that makes a WHOLE lot more sense. I’ll give it a go.

1 Like

aaaah thanks a billion, you saved me from headaches upon headaches!

Humbly, Expistic

1 Like