So I am trying to simplify this script and somebody told me to make them into functions when the onTouch event is fired. I tried simplifying the script but my question is how would I make it into functions? the issue is I didn’t know how to get all the parts being touched so I made different local scripts for every single part.
Here is the script:
local Cooldown = false
local Part = script.Parent
print("Script running")
Part.Touched:Connect(function(Hit)
print("Event fired")
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and not Cooldown then
Cooldown = true
print("Found a player")
local Humanoid = Hit.Parent.Humanoid
if Humanoid.Health == 0 then
print("Making the part visible")
for Loop = 1, 10 do
Hit.Transparency -= 0.1
wait(.1)
end
Cooldown = false
else
print("The humanoid's health is not zero!")
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local Arm = Player.Character:FindFirstChild("Left Arm2")
if Arm then
for i = 1,0,-0.1 do --first number its where the loop starts, second is where it ends, and third, how much to add to i per loop. i starts as 1, then 0.9, and so on, until it reaches 0
Arm.Transparency = i
wait(0.1)
end
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local Arm2 = Player.Character:FindFirstChild("Right Arm2")
if Arm2 then
for i = 1,0,-0.1 do
Arm2.Transparency = i
wait(0.1)
end
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local Leg = Player.Character:FindFirstChild("Left Leg2")
if Leg then
wait(2.7)
for i = 1,0,-0.1 do
Leg.Transparency = i
wait(0.1)
end
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local Leg2 = Player.Character:FindFirstChild("Right Leg2")
if Leg2 then
wait(2.7)
for i = 1,0,-0.1 do
Leg2.Transparency = i
wait(0.1)
end
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local Torso = Player.Character:FindFirstChild("Torso2")
if Torso then
wait(3.2)
for i = 1,0,-0.1 do
Torso.Transparency = i
wait(0.1)
end
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local Head = Player.Character:FindFirstChild("HeadWhite")
if Head then
wait(3.6)
for i = 1,0,-0.1 do
Head.Transparency = 1
wait(0.1)
Head:FindFirstChild("Decal").Transparency = 0
end
end
end
end)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local FirstHead = Player.Character.Head:FindFirstChild("face")
if FirstHead then
wait(3.5)
FirstHead.Transparency = 1
end
end
end)