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.
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
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
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)