Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do fix this script so when one dummy is killed the next time I press U it kills the next dummy?

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:

local commandTest = game.Workspace.CommandTestDummies:FindFirstChild("KillTest")
local userInput = game:GetService("UserInputService")
local human = commandTest:FindFirstChild("Humanoid")

userInput.InputBegan:Connect(function(inputBegan, gameProccesed)
	if inputBegan.KeyCode == Enum.KeyCode.U then
		if human ~= nil then
			human.Health = human.Health - 25
		end
	end
end)

1 answer

✓ Accepted answer

Create "dummys" with a name followed by a number starting at 0.

local Index = 0
local commandTest = game.Workspace.CommandTestDummies:FindFirstChild("Name0")
local userInput = game:GetService("UserInputService")
local human = commandTest:FindFirstChild("Humanoid")

userInput.InputBegan:Connect(function(inputBegan, gameProccesed)
    if inputBegan.KeyCode == Enum.KeyCode.U then
        if human ~= nil then
            human.Health = human.Health - 25
	else
		Index = Index + 1
		commandTest = game.Workspace.CommandTestDummies:FindFirstChild("Name"..Index)
		human = commandTest:FindFirstChild("Humanoid")
        end
    end
end)


This discussion is preserved as it was. New answers, comments, and votes are closed.