If statement not working properly?

Hello!, Currently I’m doing a melee game and to detect hits I use a magnitude check on all player’s HumanoidRootParts and if they are in range I deal dammage to them (I know it’s bad since if 2 players are near then the player who is first inside the folder will get hit and not the closest, If you know a way to help me with this too then put it in it would be apreciated) So the problem comes when you dont want the system to detect yourself and deal dmg to yourself because you are close to the range so my solution was adding an if statement that compares the Character retrieved on the on the folder with the player who is firing the code and if they are the same it will ignore it and compare the rest of the folder until it finds one, But for some reason even if they arent the same it still ignores it.

Code:

– Raycast Attack function

local function CreateRayCastHitbox()

if RayCastAttack.Value then
	
	for i, CharacterRetrieved in pairs(InGamePlayersFolder:GetChildren()) do

		local DistanceBetweenCharacterRetrivedAndMyCharacter = (CharacterRetrieved.HumanoidRootPart.Position - MyCharacter.HumanoidRootPart.Position).Magnitude
		
		print("DISTANCE",CharacterRetrieved.Name, DistanceBetweenCharacterRetrivedAndMyCharacter)

		if DistanceBetweenCharacterRetrivedAndMyCharacter <= 10 and not CharacterRetrieved == MyCharacter then

			print("HIT", CharacterRetrieved.Name)

			RayCastRemote:FireServer(CharacterRetrieved.HumanoidRootPart)
			
			return 

		else
			
			print("EVENT DENIED", CharacterRetrieved.Name)
			
		end

	end

end

Output:

21:17:02.114 DISTANCE RespawningDummy 26.456268310547 - Client - FireAxeLocalScript:75
21:17:02.114 CCTVStudios - Client - FireAxeLocalScript:77
21:17:02.114 EVENT DENIED RespawningDummy - Client - FireAxeLocalScript:89
21:17:02.114 DISTANCE CCTVStudios 0 - Client - FireAxeLocalScript:75
21:17:02.114 CCTVStudios - Client - FireAxeLocalScript:77
21:17:02.114 EVENT DENIED CCTVStudios - Client - FireAxeLocalScript:89

Also I have tried using Instance.Name == Instance.Name too and it didnt worked

Can I see how the MyCharacter variable is defined?
Actually nevermind if the was the problem there would be more errors but still im curious on how its defined

My Character is actually deffined on the local script like this:

local MyCharacter = ´Players.LocalPlayer.Character

And sadly yet again the forum wasnt able to help me and im stuck on this hole completely, Theres no way I can find the issue because from what I tried to find no one else has the same issue so my only go is scrapping my system entirely and think of a new idea.

should be
not (CharacterRetrieved == MyCharacter)
and even better
CharacterRetrieved ~= MyCharacter

difference? lua prioritizes not over == so basically yours works like this
(not CharacterRetrieved) == MyCharacter

however you already said that you scrapped it so i think this is useless (unless you put it back)

2 Likes

You just saved my life man, I was about to, Thank you very much, Now I should mark this as solved, Have a nice day :sunglasses:

1 Like