I want it so that you can kill the zombies and it doesn’t interfere with the already anti-team kill part of the function. Here is my server script for my gun.
script.Parent:WaitForChild("Fire").OnServerEvent:Connect(function(player, target, Gun)
local attackerPlayer = game.Players:GetPlayerFromCharacter(player.Character)
local targetPlayer = game.Players:GetPlayerFromCharacter(target.Parent)
if attackerPlayer and targetPlayer and attackerPlayer.Team ~= targetPlayer.Team then
if target.Parent:FindFirstChild("Humanoid") then
target.Parent.Humanoid:TakeDamage(15)
end
end
end)
script.Parent:WaitForChild("FireEffect").OnServerEvent:Connect(function()
local part = script.Parent.Part
local muzzle = part:FindFirstChild("Muzzle")
local muzzleFlash = part:FindFirstChild("Muzzle Flash")
local fireSound = script.Parent:FindFirstChild("FireSound")
if fireSound then
fireSound:Play()
end
if muzzle then
muzzle.Enabled = true
end
if muzzleFlash then
muzzleFlash.Enabled = true
end
task.wait(0.1)
if muzzle then
muzzle.Enabled = false
end
if muzzleFlash then
muzzleFlash.Enabled = false
end
end)
If the zombie you are referring to is a “bot”, then: local targetPlayer = game.Players:GetPlayerFromCharacter(target.Parent)
logically, the value of “targetPlayer” would be nil. Because the function “:GetPlayerFromCharacter(target.Parent)” will not find a player and will return nil.
The corrected code would be:
script.Parent:WaitForChild("Fire").OnServerEvent:Connect(function(player, target, Gun)
local attackerPlayer = game.Players:GetPlayerFromCharacter(player.Character)
local targetPlayer = game.Players:GetPlayerFromCharacter(target.Parent)
if targetPlayer ~= nil then
if attackerPlayer.Team ~= targetPlayer.Team then
if target.Parent:FindFirstChild("Humanoid") then
target.Parent.Humanoid:TakeDamage(15)
end
end
else
if target.Parent:FindFirstChild("Humanoid") then
target.Parent.Humanoid:TakeDamage(15)
end
end
end)
script.Parent:WaitForChild("FireEffect").OnServerEvent:Connect(function()
local part = script.Parent.Part
local muzzle = part:FindFirstChild("Muzzle")
local muzzleFlash = part:FindFirstChild("Muzzle Flash")
local fireSound = script.Parent:FindFirstChild("FireSound")
if fireSound then
fireSound:Play()
end
if muzzle then
muzzle.Enabled = true
end
if muzzleFlash then
muzzleFlash.Enabled = true
end
task.wait(0.1)
if muzzle then
muzzle.Enabled = false
end
if muzzleFlash then
muzzleFlash.Enabled = false
end
end)
Don’t take it the wrong way. But this is not thinking outside the box. Especially when it comes to game development, we really have to think OUTSIDE THE BOX.
Your code assumes that the zombie must have the name “Zombie”. But if you really think outside the box, there can be several types of zombies (even if the developer is not thinking about more types of zombies, they should always think outside the box, in the future they may want to change that. Stop and think, wouldn’t it be boring a game with only 1 type of zombies? lol). This also gives code flexibility, so the player can change the name of the Model however they want, because it will not affect the code in any way. With the check you did on the name, it would be restricted to Models with the name “Zombie” but you may want there to be more: “Zombie Big”…“Zombie Small”.
That’s my point of view. Of course your code is correct in your point of view. But in my “outside the box” thinking it’s another way. It’s a feeling of embracing the uncertainty of the future, and dealing with it well.
Example:
If I’m making a system for a zombie, I’m going to embrace the uncertainty of wanting more zombies in the future. And then I will adapt my system to dealing damage (as in this case) to any other type of zombies.
And thinking even more outside the box, I would do many more checks like this: checking the type of zombie, the type of damage to be done, etc., even if the game won’t have any system that requires this for now.
And that’s it, I just wanted to disagree with you a little when you said “think outside the box” take it as an opinion, not as a contradiction against you
To add to this, this can easily be condensed into one if statement by following some boolean logic:
if attackerPlayer and target.Parent:FindFirstChild("Humanoid") and (targetPlayer and attackerPlayer.Team ~= targetPlayer.Team or not targetPlayer) then
target.Parent.Humanoid:TakeDamage(15)
end
That’s too long-winded. It’s best to use either of the two patterns:
if targetPlayer and targetPlayer.Team == attackPlayer.Team then
return
end
local humanoid = target.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(15)
end
Or:
local friendlyFire = targetPlayer and targetPlayer.Team == attackPlayer.Team
local humanoid = target.Parent:FindFirstChildOfClass("Humanoid")
if humanoid and not friendlyFire then
humanoid:TakeDamage(15)
end
Yes I totally agree on you but since he was making a basic gun script I didnt wanted to give the info directly. Since he has to figure it out himself for a better learning process. Also I think you are the paragraph guy
Regarding whether I’m a “paragraph guy” or not, the only thing I have to say is: think what you want about me, it’s just your projection onto me. I said what I said because I know what I’m talking about.
This thing about learning “alone” is, and only, the phrases of today’s degrading society(like those phrases from stupid coaches). At school we learn from our teachers, and this is brutally necessary.
If your teacher gives you the volumetric mass formula, you will learn it and study it at home. He won’t give you half the formula, he will give you the complete formula for you to study AT HOME.
You gave that code solution in the simplest way. If he uses that code, it would be like using half the formula. And he would probably follow this error if he wanted to add more zombies, he would create more “ifs” and contaminate the code with unnecessary lines.
At no point is your code incorrect, but it’s not the best way to do it. Like my code, there are definitely better ways to do it. Nobody is perfect and that is what distinguishes us from others.