I am working on a advanced limb health system with additional effects and properties based on limb loss. It works fine but I was thinking of releasing this to the public later on and was wondering if I could make this function cleaner? Here is the function:
-- This function controls limb loss
-- Uses 2 properties to control who is losing the limb, and the specific limb to lose
-- This also controls the additional effects that happen when losing them
function LimbHandler.LoseLimb(char, limb)
if limb == "Left Arm" and char.Torso:FindFirstChild("Left Shoulder") then
char.Torso["Left Shoulder"]:Destroy()
local sound = ScreamSounds[math.random(#ScreamSounds)]
sound:Play()
char.Torso.LArmGore.Transparency = 0
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char.Torso.LArmGore.Position)
for i = 1,10 do
char.Humanoid.Health = char.Humanoid.Health - 3
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char["Torso"].Position)
wait(1)
end
elseif limb == "Right Arm" and char.Torso:FindFirstChild("Right Shoulder") then
char.Torso["Right Shoulder"]:Destroy()
local sound = ScreamSounds[math.random(#ScreamSounds)]
sound:Play()
char.Torso.RArmGore.Transparency = 0
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char.Torso.RArmGore.Position)
for i = 1,10 do
char.Humanoid.Health = char.Humanoid.Health - 3
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char["Torso"].Position)
wait(1)
end
elseif limb == "Head" and char.Torso:FindFirstChild("Neck") then
char.Torso["Neck"]:Destroy()
local sound = ScreamSounds[math.random(#ScreamSounds)]
sound:Play()
char.Torso.HeadGore.Transparency = 0
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char.Torso.HeadGore.Position)
elseif limb == "Left Leg" and char.Torso:FindFirstChild("Left Hip") then
char.Torso["Left Hip"]:Destroy()
local sound = ScreamSounds[math.random(#ScreamSounds)]
sound:Play()
char.Humanoid.WalkSpeed = 13
char.Torso.LLegGore.Transparency = 0
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char.Torso.LLegGore.Position)
if char.Torso:FindFirstChild("Right Hip") == nil then
while char.Humanoid.Health > 0 do
wait(.1)
char.Humanoid.Health = char.Humanoid.Health - 5
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(1,2), char["Torso"].Position)
end
else
for i = 1,5 do
char.Humanoid.Health = char.Humanoid.Health - 3
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char["Torso"].Position)
wait(1)
end
end
elseif limb == "Right Leg" and char.Torso:FindFirstChild("Right Hip") then
char.Torso["Right Hip"]:Destroy()
local sound = ScreamSounds[math.random(#ScreamSounds)]
sound:Play()
char.Humanoid.WalkSpeed = 13
char.Torso.RLegGore.Transparency = 0
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char.Torso.RLegGore.Position)
if char.Torso:FindFirstChild("Left Hip") == nil then
while char.Humanoid.Health > 0 do
wait(.1)
char.Humanoid.Health = char.Humanoid.Health - 5
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(1,2), char["Torso"].Position)
end
else
for i = 1,5 do
char.Humanoid.Health = char.Humanoid.Health - 3
game.ReplicatedStorage.Events.GoreEvent:FireAllClients(math.random(2,3), char["Torso"].Position)
wait(1)
end
end
end
end
I explained in the code block what it does, but if you want to know the additional properties Ill explain them:
If you lose both legs you bleed out quickly and are guaranteed to die
You bleed for a few seconds when you lose a limb
You bleed more if its an arm
You walk slower if you lose a leg
Maybe I should make a function for the bleeding? Sorry if the code is super awful I am still kinda new to scripting.
Also I forgot to mention the goreevent is just for blood coming out of the torso positioned where the limb was.