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

Script that supposed to decrease health every .2 seconds not working?

local redpart = script.Parent
local Player = game.Players.LocalPlayer
local  character = Player:GetPlayerFromCharacter(character)
local humanoid = character:FindFirstChild("Humanoid")


redpart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		while humanoid.Health > 0 do
            wait(0.2)
			humanoid:TakeDamage(10)
		end
	end
end)

Ive tried doing this in global and local scripts but Player.Character kept erroring for me on global so just did local (my script isnt working on either though)

2 answers

✓ Accepted answer

Hello Bloxerventure!

You can only define player in local scripts and define if a part touched something in scripts. Here is a script that you should instead use in a global script

local redpart = script.Parent

redpart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		while hit.Parent.Humanoid.Health > 0 do
			wait(0.2)
			hit.Parent.Humanoid:TakeDamage(10)
		end
	end
end)

2 comments

0

Thank you for your awnser but also would you happen to know how to stop the damage when TouchEnded tried doing this but wasnt working

0

I mean you could try to make a new bool value in the part and call it lets say 'Touched'. In the while loop you could say: while hit.parent.Humanoid.Health >0 and redpart.Touched.Value == false do. Another script could change the bool value in the touch ended function.

I don't understand why you need to do Player.Character or the other variables for a Touched event to damage someone but you can simply do this.

local redpart = script.Parent

redpart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local humanoid = hit.Parent.Humanoid
		while humanoid.Health > 0 do
			wait(0.2)
			humanoid:TakeDamage(10)
		end
	end
end)

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