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

Damage 5 Every Second?

okay, so im making a game when you touch the brick, it will damage you by 5 every second. but when you stop touching it, it stops damaging you. but my script doesn't stop it from damaging the player AND it doesn't even damage it every second. i cant seem to figure out how to do this though. please help!

function touch(hit)
	while true do
		hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -5
		wait (1)
	end
end

script.Parent.Touched:connect(touch)

1 comment

0

try this:

2 answers

✓ Accepted answer

You have had some really strange answers. This is probably the simplest solution:


--Using a debounce so happens once a second:
debounce = false

--Touched Function:

script.Parent.Touched:Connect(function(hit)
	
	
	--Checks if it is a human:
	if hit.Parent:FindFirstChild("Humanoid") then
	
		--Checks if the debounce is true (returns if it is)
		if debounce == true then
			return
		end
		
		--Sets the debounce to true (as now touching):
		debounce = true
		--Changes health to current health - 5
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 5
	
		--Waiting Time:
		wait(1)
		--Waits a second and runs again.
		debounce = false	
	end
	
	
end)

Added comments within the script but if you have any questions let me know.

1 comment

0

Works!

try this: ` local function getPlayerFromCharacter(character) for _, player in pairs(game:GetService("Players"):GetPlayers()) do if player.Character == character then return player end end end

script.Parent.Touched:connect(function(hit) if hit and hit.Parent then Character = hit.Parent player = getPlayerFromCharacter(hit.Parent) if player == nil then print("reward: player does not exist") else player.Character.Health = player.Character.Health - 5 wait(1) end end end)

`

1 comment

0

Nope didnt work, it wouldnt even lower the players hp

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