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

Workspace.SCP-075.SCP.Part.Script:4: attempt to index nil with 'Health' server script help?

function onTouched(part) local h = part.Parent:findFirstChild("Humanoid")

	h.Health = h.Health -25
            wait(2)
	h.Health = h.Health -5

end

script.Parent.Touched:connect(onTouched)

1 comment

0

Please stop spamming the question tab

1 answer

It seems the parent of the part does not have an humanoid maybe? try this:

function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") then
		local h = part.Parent.Humanoid

		h.Health = h.Health - 25
		wait(2)
		h.Health = h.Health - 5
	else
		print(part.Parent.Name, "does not have an humanoid!")
	end
end

script.Parent.Touched:Connect(onTouch)

Keep in mind there is no cooldown between touches, so the Touched event will fire infinitely as long as long as the part is being touched, if you don't want that to happen, you can add a cooldown using debounce or something, like this:

local TimeBetweenTouches = 2

local debounce = false

function onTouch(part)
	if debounce == false then
		debounce = true
		
		if part.Parent:FindFirstChild("Humanoid") then
			local h = part.Parent.Humanoid

			h.Health = h.Health - 25
			wait(2)
			h.Health = h.Health - 5
		else
			print(part.Parent.Name, "does not have an humanoid!")
		end
		
		wait(TimeBetweenTouches)
		debounce = false
	end
end

script.Parent.Touched:Connect(onTouch)

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