I have the problem with the attack of my zombie can you help me please here is the script:
01 | local rarm = script.Parent:WaitForChild( "Right Arm" ) |
02 | local larm = script.Parent:WaitForChild( "Left Arm" ) |
03 |
04 | function dmg(hit) |
05 | if hit.Parent ~ = nil then |
06 | local hum = hit.Parent:findFirstChild( "Humanoid" ) |
07 | if hum ~ = nil then |
08 | hum.Health = hum.Health - 10 |
09 | end |
10 | end |
11 | end |
12 |
13 | rarm.Touched:connect(dmg) |
14 | larm.Touched:connect(dmg) |
Thanks
The function <Object>:WaitForChild(<thing>,<timeout>)
as you can see it has two arguments One for the name of the object you're looking for and a timeout (in seconds). So if you have no timeout in your script's it will hang for a while until the timeout error Infinite yield
is thrown.
The proper way to use the function is as follows:
--Wrong way (kinda)
01 | local rarm = script.Parent:WaitForChild( "Right Arm" ) -- this will hang the script if it cannot find an object named "Right Arm", Though if it finds it then it will continue as normal! |
02 |
03 | local larm = script.Parent:WaitForChild( "Left Arm" ) -- this will hang the script if it cannot find an object named "Left Arm", Though if it finds it then it will continue as normal! |
04 |
05 | function dmg(hit) |
06 | if hit.Parent ~ = nil then |
07 | local hum = hit.Parent:findFirstChild( "Humanoid" ) |
08 | if hum ~ = nil then |
09 | hum.Health = hum.Health - 10 |
10 | end |
11 | end |
12 | end |
13 |
14 | rarm.Touched:connect(dmg) |
15 | larm.Touched:connect(dmg) |
Correct Way:
01 | local rarm = script.Parent:WaitForChild( "Right Arm" , 10 ) -- this will suspend the script until the object("Right Arm") appears in script.Parent. If it doesn't find it after 10 secs it will make this equal nil |
02 |
03 | local larm = script.Parent:WaitForChild( "Left Arm" , 10 ) -- this will suspend the script until the object("Left Arm") appears in script.Parent. If it doesn't find it after 10 secs it will make this equal nil |
04 | function dmg(hit) |
05 | if hit.Parent ~ = nil then |
06 | local hum = hit.Parent:findFirstChild( "Humanoid" ) |
07 | if hum ~ = nil then |
08 | hum.Health = hum.Health - 10 |
09 | end |
10 | end |
11 | end |
12 | --// if any of the above checks come back nil, this table will be filled with nil's and since you cannot have a nil property of a table the length of the table will be less than the number of checks! |
13 | local ChkTable = { rarm,larm } |
14 | if (#ChkTable < 2 ) then |
15 | if (rarm = = nil ) then warn( "Error: Right Arm is nil!" ) end |
16 | if (larm = = nil ) then warn( "Error: Left Arm is nil!" ) end |
17 | error ( "Error: Something went wrong with WaitForChild() Checks as not all objects are found in the check table!" ) |
18 | end |
19 | rarm.Touched:connect(dmg) |
20 | larm.Touched:connect(dmg) |
ask if your unsure on anything and Hope this helps! :)