hi i need help please my zombies named "noobs" attacking each others here is the script
01 | local rarm = script.Parent:FindFirstChild( 'Right Arm' ) or script.Parent:FindFirstChild( 'RightUpperArm' ) |
02 | local larm = script.Parent:FindFirstChild( 'Left Arm' ) or script.Parent:FindFirstChild( 'LeftUpperArm' ) |
03 | local head = script.Parent:FindFirstChild( "Head" ) |
04 |
05 | local debounce = false |
06 |
07 | local function dmg(hit) |
08 | if (hit.Parent ~ = nil ) and ( not debounce) then |
09 | debounce = true |
10 | local hum = hit.Parent:FindFirstChild( 'Humanoid' ) |
11 | if (hum ~ = nil ) then |
12 | hum.Health - = 200 |
13 | end |
14 | wait( 1 ) |
15 | debounce = false |
Hi, when function dmg() is run, it checks hit.Parent for humanoid:
1 | 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:
1 | 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:
01 | local rarm = script.Parent:FindFirstChild( 'Right Arm' ) or script.Parent:FindFirstChild( 'RightUpperArm' ) |
02 | local larm = script.Parent:FindFirstChild( 'Left Arm' ) or script.Parent:FindFirstChild( 'LeftUpperArm' ) |
03 | local head = script.Parent:FindFirstChild( "Head" ) |
04 |
05 | local debounce = false |
06 |
07 | local function dmg(hit) |
08 | if (hit.Parent ~ = nil ) and ( not debounce) and ( not hit.Parent.Name = = "noobs" ) then |
09 | debounce = true |
10 | local hum = hit.Parent:FindFirstChild( 'Humanoid' ) |
11 | if (hum ~ = nil ) then |
12 | hum.Health - = 200 |
13 | end |
14 | wait( 1 ) |
15 | debounce = false |
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...
1 | local inst = script.Parent |
2 | if not game:GetService( "Players" ):GetPlayerFromCharacter(inst) then |
3 | print ( "No player" ) |
4 | else |
5 | print ( "There is a player here" ) |
6 | 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.