Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i use the same function in different scripts?

Hello everyone, so for example I've made this simple function of brick that kills you if you touch it.

function Touchies(mb)
 if mb and mb.Parent and mb.Parent:FindFirstChild("Humanoid") then
mb.Parent.Humanoid.Health = 0
	end
end


And how do i make it that I can initialize this function for different scripts with:

script.Parent.Touched:connect(Touchies)

Instead of copying same part of code again?

1 answer

✓ Accepted answer

Connect the function with a bindable event in one script.

Script A (this can be used for whichever parts you want)

script.Parent.Touched:Connect(function(mb)
local humanoid = mb.Parent:FindFirstChild("Humanoid")
if mb and mb.Parent then
if humanoid then
Event:Fire(humanoid)
		end
	end
end)

Script B

Event.Event:Connect(function(humanoid)
humanoid.Health = 0
end)

1 comment

0

Understood, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.