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

Make Block Constantly Damage Player on Touch?

I have a block where I want it to do damage to the player at a constant rate (like 5 damage a second). Basically, when the player is on the block, the block will remove 5 health from that player each second. I want it so that it will still do damage to the player at a constant rate regardless of whether or not the player is standing still. I would also want the player to be slowed down when the player is on it. How would I do it? Here's what I have so far:

local CollectionService = game:GetService("CollectionService")
local part = CollectionService:GetTagged("DamagePart")

for _, TaggedPart in pairs(part) do
	local onblock = false
	function onTouched(hit)
		if onblock == false then
			onblock = true
			local h = hit.Parent:findFirstChild("Humanoid")
			h.WalkSpeed = 8
			while onblock == true do 
				h.Health = h.Health - 5
				wait(1)
			end
			onblock = false
		end
	end

	function stopdamage(hit)
		onblock = false
		hit.Parent:findFirstChild("Humanoid").WalkSpeed = 16
	end
	TaggedPart.Touched:Connect(onTouched)
	TaggedPart.TouchEnded:Connect(stopdamage)
end

This is quite buggy and does inconsistent damage. It also will sometimes not do damage when the player is standing still. If you could tell me what I did wrong and paste a working code snippet in I would really appreciate it. Thanks for your help :)

Also while you're at it can you please answer this question for me too? scriptinghelpers.org/questions/115188/bullets-are-not-going-through-objects-without-collisions

1 answer

✓ Accepted answer

Put a boolean value into StarterPlayer > StarterCharacterScripts. Name it TouchingDamageBlock or something like that. Now, put a script in StarterCharacterScripts. In the script write:

local human = script.Parent:FindFirstChild("Humanoid")

if human then
	while true do
		wait(1)
		if script.Parent.TouchingDamageBlock.Value == true then
			human.Health = human.Health - 5
		end
	end
end

Then, put this script in your block.

script.Parent.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		hit.Parent.TouchingDamageBlock.Value = true
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		hit.Parent.TouchingDamageBlock.Value = false
	end
end)

1 comment

0

Thanks! I moved the wait(1) line after the if statement so that way players can't avoid taking damage by jumping. Other than that it works as expected :)

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