Most efficient way to make a bleed system?

So I am looking to make a bleeding system when you touch a part, I want to the player to loose health every second, for 5 seconds. I want to use a module script for this, but how can I take a part as a parameter in my function? Is there anyway at all to do this? If not, what are some alternatives?

Here is my basic code, It’s fresh out of the code oven, I made it like 5 minutes ago, haven’t tested it. Feedback is appreciated!

local module = {}

function Bleed(hit, part)
	local hitT = hit.Parent:GetChildren()
	for i,v in ipairs(hitT) do
		if v:IsA("Humanoid") then
			while wait(1) do
				v.Health -= 5

			end
		end
	end
end

return module

Yes I know that it will damage the player forever, I just want to be able to use this.

2 Likes

Here’s something I’ve come up with, not the best but it should work.

local Bleeder = {}

function Bleeder:Bleed(Hit, Length)
	local Character = Hit.Parent
	if not Character then return false end
	local Humanoid = Hit:FindFirstChildOfClass('Humanoid')
	if not Humanoid then return false end
	local t = 0
	while t >= Length do
		Humanoid.Health -= 5
		t += wait(1)
	end
	return true
end

return Bleeder
1 Like

Thank you, but how do I add that to a part, do I just do this:

--ServerScript
local bleedsystem = require(game.ServerScriptService.BleedSytem)

script.Parent:Bleed()

end

Or is it something else?

1 Like
local BleedModule = {}

function BleedModule.Bleed(Hit)
	local Character = Hit.Parent
	if not Character then return false end
	local Humanoid = Hit:FindFirstChildOfClass('Humanoid')
	if not Humanoid then return false end
	local t = 0
	while t >= 5 do
		Humanoid.Health -= 5
		t += wait(1)
	end
	return true
end

function BleedModule.ListenForTouch(part)
    return part.Touched:Connect(BleedModule.Bleed)
end

return BleedModule
--Server Script
local BleedModule = require(BleedModulePath)
local connection = BleedModule.ListenForTouch(part)

You should also add a debounce so players cant bleed multiple times from the same part in the same time frame

2 Likes

Where do I put the stuff under the --Server Script part? I will add debounce also.

1 Like

It says that the module didn’t return exactly one value, and it doesn’t work, do you know why is that?

1 Like

The module is missing return BleedModule at the bottom, add that one line in and it should be working.

2 Likes

It just says Attempt to index nil with 'Touched' in the Bleed Module at line 18.

1 Like

Did you pass the part alongside the .ListenForTouch function?

module help

I don’t really understand what you mean, so here is that part of the code.

Inside of the server script, where you do BleedModule.ListenForTouch(part)

Attempt to index nil with touched means that the part was not passed to the function. Make sure you are passing in a valid part

Here is the script in the server script. It’s inside of a part:
module error

or


local module = require(game.ServerScriptService.BleedSystem2)

script.Parent.Touched:Connect(module.Bleed)

You can just do in the module script,

local BleedModule = {}

function BleedModule.Bleed(humanoid)

    local t = 0

    while tonumber(humanoid.Health) > 0 do
	    humanoid.Health -= 5
	    t += wait(1)
    end
    return true
end

return BleedModule

And the server script,

local debounce = false
game.Workspace.Tutorial.Touched:Connect(function(hit)
    if debounce == false then
	    debounce = true
	
	    local module = require(game.ReplicatedStorage.ModuleScript)
     
        module.Bleed(hit.Parent:WaitForChild("Humanoid"))

	    debounce = false
    end
end)
1 Like

It works, but how do I make the bleeding stop after 5 seconds? Also what is tonumber?

So you can change those lines in module script

while tonumber(humanoid.Health) > 0 do
    humanoid.Health -= 5
    t += wait(1)
end

To

while tonumber(humanoid.Health) > 0 and t < 5 do
	humanoid.Health -= 5
	t += wait(1)
end

Oh, it is just a keyword to convert strings to numbers. You don’t need to use it here because humanoid’s health is already a number.

1 Like

It does work, but whenever it stops, I get an error, whenever I restart it, I get more errors.

What is the error? Can I please see it?

1 Like

more errors a

The one on the top is when I restart it, the one on the bottom is when the script ends.

This is probably because of the debounce because it is boolean but I don’t know why the second one is occuring.

1 Like