hi i need help please my zombies named "noobs" attacking each others here is the script
local rarm = script.Parent:FindFirstChild('Right Arm') or script.Parent:FindFirstChild('RightUpperArm') local larm = script.Parent:FindFirstChild('Left Arm') or script.Parent:FindFirstChild('LeftUpperArm') local head = script.Parent:FindFirstChild("Head") local debounce = false local function dmg(hit) if (hit.Parent ~= nil) and (not debounce) then debounce = true local hum = hit.Parent:FindFirstChild('Humanoid') if (hum ~= nil) then hum.Health -= 200 end wait(1) debounce = false end end rarm.Touched:Connect(dmg) larm.Touched:Connect(dmg) head.Touched:Connect(dmg)
Hi, when function dmg() is run, it checks hit.Parent for humanoid:
local hum = hit.Parent:FindFirstChild('Humanoid')
Your zombies likely have humanoids in them, so they pass the check. You should make another if statement such as:
if not hit.Parent.Name == "noobs" then -- or whatever the zombie name is
to make sure it doesn't run if it hits another zombie. This assumes you want to damage any humanoid. If it's just players, use DeceptiveCaster's solution.
Edit:
local rarm = script.Parent:FindFirstChild('Right Arm') or script.Parent:FindFirstChild('RightUpperArm') local larm = script.Parent:FindFirstChild('Left Arm') or script.Parent:FindFirstChild('LeftUpperArm') local head = script.Parent:FindFirstChild("Head") local debounce = false local function dmg(hit) if (hit.Parent ~= nil) and (not debounce) and (not hit.Parent.Name == "noobs") then debounce = true local hum = hit.Parent:FindFirstChild('Humanoid') if (hum ~= nil) then hum.Health -= 200 end wait(1) debounce = false end end rarm.Touched:Connect(dmg) larm.Touched:Connect(dmg) head.Touched:Connect(dmg)
GetPlayerFromCharacter()
is a function of the Players
service that returns a Player object associated with the given Instance, given that there is a client associated with the Instance. If no Player object can be obtained from the Instance, the function returns nil.
This is extremely useful for zombie AI and you should absolutely use it (I use it myself). You have to counteract the function, such as doing...
local inst = script.Parent if not game:GetService("Players"):GetPlayerFromCharacter(inst) then print("No player") else print("There is a player here") end
not
will essentially check if the function does not return a player; this is equivalent to false
, and in this case it refers to not true
, which is the same exact thing.