I made a script where when you press U, it damages one of the dummies. However, the problem is that it only damages one dummy and not any other. How do I fix this script?
The script:
01 | local commandTest = game.Workspace.CommandTestDummies:FindFirstChild( "KillTest" ) |
02 | local userInput = game:GetService( "UserInputService" ) |
03 | local human = commandTest:FindFirstChild( "Humanoid" ) |
04 |
05 | userInput.InputBegan:Connect( function (inputBegan, gameProccesed) |
06 | if inputBegan.KeyCode = = Enum.KeyCode.U then |
07 | if human ~ = nil then |
08 | human.Health = human.Health - 25 |
09 | end |
10 | end |
11 | end ) |
Create "dummys" with a name followed by a number starting at 0.
01 | local Index = 0 |
02 | local commandTest = game.Workspace.CommandTestDummies:FindFirstChild( "Name0" ) |
03 | local userInput = game:GetService( "UserInputService" ) |
04 | local human = commandTest:FindFirstChild( "Humanoid" ) |
05 |
06 | userInput.InputBegan:Connect( function (inputBegan, gameProccesed) |
07 | if inputBegan.KeyCode = = Enum.KeyCode.U then |
08 | if human ~ = nil then |
09 | human.Health = human.Health - 25 |
10 | else |
11 | Index = Index + 1 |
12 | commandTest = game.Workspace.CommandTestDummies:FindFirstChild( "Name" ..Index) |
13 | human = commandTest:FindFirstChild( "Humanoid" ) |
14 | end |
15 | end |
16 | end ) |