I tried multiple times but failed
This is the function that detects for a hit (also this gun uses ray casting).
The main point of this question is to find out how to make anti team kill in this function.
local function hit(plr, part)
if not canShoot() then return end
if not part then return end
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")
if rootPart then
if (rootPart.CFrame.p - part.CFrame.p).magnitude <= range.Value then
local humanoid
if part.Name == "Handle" then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
else
humanoid = part.Parent:FindFirstChild("Humanoid")
end
if humanoid and humanoid.Health > 0 then
local newDamage = damage.Value
if part.Name == "Head" then
newDamage = newDamage * headMultiplier.Value
local soundCopy = headshotSound:Clone()
soundCopy.Parent = part
soundCopy:Play()
end
humanoid:TakeDamage(newDamage)
end
end
end
end

18 comments
wait where does this go again?
I’d put it after the hit is detected and after you got the character
Don’t forget to fill in the damage in the if statement. Alternatively you can just return if they are on the same team with no damage being done since you exited
Soooo something like this? https://gyazo.com/350cfcd1c874f63c14d2dda309167420
Seems resonable
Doesn't work. : /
Any errors? Any leads? From what I see why did you choose the team color == 8?
nope, no errors at all, also i have no idea but i did chose a team color, the 8 isnt there anymore
From what I can see, your if statements are a bit messed up. that "return end" should just have return and then move on to the else statement. Right now the else is saying "If there isn't a humanoid then try to do damage."
Changed return end to return but it still doesn't work (it doesnt do damage)
If you want send an updated screenshot, also if any new errors have occurred that may be helpful as well. Sorry this is taking so long!
Alrighty. https://gyazo.com/595a4924936f7c12803f00fc394ded40
It might be a good time to try some debugging methods, such as print debugging. We need to find out where exactly this code is stopping. So sprinkle some prints around and lets figure out where it stops. It should be noted that this could potentially be a problem if you have this in a local script and you are running it with filtering enabled, you'll need to set up an event to do damage on the
server
This is in a server script btw.
I put some prints in the if and else and it does not print anything when hitting teammates and enemies
That likely means it's not even getting to that point! You should try putting prints higher up in the function, like before CanShoot, etc.
I can probably team create you then, cause this is getting confusing atm. Or I can use codeshare.