I Have a script with my code that detects if any player in the server touches a part. If the player touches the part. it Runs the code using hit.parent.humanoid Bla bla bla. But I have another script which I want to Also make the selected player run the script. but the script only takes ontouched input. not remotevent or fire? how could I run it both ways without breaking?
for _, Part in pairs(Scripted) do
if Part:IsA("BasePart") then
Part.Touched:Connect(
function(hit)
end
)
end
end
Thats Just the First Part of the script. the script is very big.
instead of having an anonymous function inside of the event, split it apart and define it. that way you can run it on touch plus being able to call said function whenever.
for _, Part in pairs(Scripted) do
if Part:IsA("BasePart") then
Part.Touched:Connect(
function(hit) -- instead of having this function declared inside of an event (anonymous) define it outside.
end
)
end
end
-- MODIFIED --
function onHit(hit) --rename it. i named it onHit for this example.
--do stuff in here
end
for _, Part in pairs(Scripted) do
if Part:IsA("BasePart") then
Part.Touched:Connect(onHit)
end
end
-- Somewhere else.
onHit(randompart) --i call onhit and it does the same thing
Your gonna have to provide a parameter if you call it yourself. just know that.
randompart is something you provide. since this originally ran off of a .Touched event which only returns a part, randompart is just a part that you defined somewhere else.
now, for functions being called in different scripts you can either use modules, or bindable functions.
Alrighty Thanks. I already have a module that handles it but I didnt notice I could just re use it. Thanks for sticking along and not one reply ghosting
welcome bro, since your using modules just know you cant bind a module function to an event. your gonna have to wrap it up inside of an anonymous function (i know ).