if game.Players:GetPlayerFromCharacter(TargetCharacter).Team ~= game.Players:GetPlayerFromCharacter(Character).Team then
Im trying to make a friendly fire system, but for some reason ~= does’nt work.
Any solution?
if game.Players:GetPlayerFromCharacter(TargetCharacter).Team ~= game.Players:GetPlayerFromCharacter(Character).Team then
Im trying to make a friendly fire system, but for some reason ~= does’nt work.
Any solution?
Wdym by “didn’t work” was there an error or is it just not working?
Also, what is your goal?
My goal is to making friendly fire, so people in team cant kill eachother.
I get no errors.
if not game.Players:GetPlayerFromCharacter(TargetCharacter).Team ==
game.Players:GetPlayerFromCharacter(Character).Team then
Well, It’d be better to see the rest of your script like what is TargetCharacter
and Character
To also make this shorter make a Players
variable.
Also, you may have used GetPlayerFromCharacter()
wrong by putting .Team (not 100% sure)
Try:
local Players = game:GetService("Players")
local Targetplr = Players:GetPlayerFromCharacter(TargetCharacter)
local Playerr = Players:GetPlayerFromCharacter(Character)
if Targetplr and Playerr then
if Targetplr.Team ~= Playerr.Team then
-- on different teams
end
end
I believe that would work and you shouldn’t put .team at GetPlayerFromCharacter, again not 100% sure
You should also make sure both players exist
The key there is how you broke it down to the single test. Then the use of ~= is valid.
The way it is wrote ~= could be returning NOT simply because one of the two statements didn’t work as intended.
This code snippet should work per se, however you should validate whether or not a player is returned from calls to the GetPlayerFromCharacter function and use variables for this. You should try to examine the rest of the code that this particular code snippet is contained in, like doing print debugging. You should also post more of the code here.
It is best not to reinvent the wheel, considering using ~=
works just fine. This is not going to work as intended since not has higher precedence than the equality operator, so this is what gets evaluated:
(not TargetPlayer.Team) == Player.Team
, which in the case TargetPlayer exists and they are associated with a team, then not TargetPlayer.Team
evaluates to false so you’re comparing a boolean and an object which may throw an error.
The solution if you desire to use the equality operator is to do not (TargetPlayer.Team == Player.Team)
but it is better to use the not equal to operator
This if statement will always return false because the not operator will only act on game.Players:GetPlayerFromCharacter(TargetCharacter).Team and not on the entire expression. For this to work it would have to be changed to
if not (game.Players:GetPlayerFromCharacter(TargetCharacter).Team ==
game.Players:GetPlayerFromCharacter(Character).Team) then
ya, I would never do it that way myself. I would break it down to a single test like Azul_Litt did.
~= is not the save all of “is not”. You will find spots where it will not work at all. I tend to just say == false or true as needed.
I tried it both ways and both don’t work … unless you use tostring() … then both worked.
local Players = game:GetService("Players")
local Character = "2112Jay"
testplayer = Players.LocalPlayer
if (tostring(testplayer) ~= Character) then
print("not the same dude")
end
I understand this isn’t a team thing but I’m not going to set all that up just for a test. I’ve had this problem in the past where I needed to add that tostring() command. This may or may not be the problem …
The types are important to take into account when doing logical operations. Teams are instances. If you compare them they can either return true or false
local team1 = game.Teams.team1
local team2 = game.Teams.team2
(team1 ~= team2) --always true
(team1 == team2) --always false
not (team1 == team2) --always true
(not team1 == team1) --always false (not only acts on team1 making not team1 equal to nil
not (team1 == team1) --always false
testplayer = Players.LocalPlayer
if (tostring(testplayer) ~= Character) then
print("not the same dude")
end
Yes this works, but it wouldnt work for comparing too team instances, unless you took the names (strings) of both instances (team1.Name == team1.Name) → true
Thank you so much! I finally got it to work.