Simplify Script using functions

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)
2 Likes

I think there’s a resource on collection service.

I think he mean’t something like this… dunno what you mean by simplify but yeah here is an example.

You can also you use Java/C# implementation of switch statement, which I also included in example

local plrservice = game:GetService("Players")

local actions = {
    [1] = function (x)
        #Include a check here 
    end;
    [2] = function (x)
        #Include a check here
    end
}

function partFunction(hit,func,karg)
    local Player = plrservice:GetPlayerFromCharacter(hit.Parent)
    if Player then
        actions[func](karg)
    end
end

local part = script.Parent
part.Touched:Connect(function(hit) 
     partFunction(hit,1 or 2 etc depending on the functions, this arg could be nil but you can refer to something that you want variable x to be in your function)
end)

Oh okay, I think I get your problem.

So basically there are 2 type of function- one is the normal function and the other known as “anonymous” function. If you use the normal function you can basically repeat the same thing over and over in one line.
However if you use anonymous function, you will need to copy paste the entire thing for each part.
(ur case^^^)

Ps: Can you mention what you are trying to accomplish here when a humanoid touches a part?

It is supposed to make a part inside the player visible. (P.S The player is a StarterCharacter with a welded part called Leg2 Arm2 Torso2 blah blah blah)

I would simply make an array with all the parts that you specifically want to check. then we something gets touched I would check if that part that is being touched is the same as the one in the array (this would be done with a for loop)

this could not be what at all you are looking for if not sorry I just had a brief look over the script

1 Like