Hello, DevForums! As the title suggests, I was wondering if a script can detect which LocalScript fired an event. I am currently clueless on how to do this, and I am not even sure if you could detect which LocalScript fired an event.
Can’t you just do script.Name
?
local Name = script.Name
local function RandomEventHere()
print("Script that fired this: "..Name)
end
I’m trying to use a script that would find two different scripts and give a unique output depending which script fired it. Sorry for not making it clear in the post!
I think @JackscarIitt 's answer works for that anyway, but if you care about the actual script instance, you can do:
-- Client
event:FireServer(script) -- 'script' isn't a pre-declared variable, it's the calling script's instance
-- Server
event.OnServerEvent:Connect(function(plr, scr)
print("Script name: " .. scr.Name)
print("Script path: " .. scr:GetFullPath())
-- You get the idea
end)
Thank you for collaborating! With both of your help, it seems to have work now!
How is it not a variable? print(getfenv().script)
doesn’t yield nil
in the output. Also, this won’t always work, if at all, since the script object may not exist on the server. Best to use the (full) name of the script. @Stxllar_Planes if you’re doing this as an anti-exploit measure then this is useless, an exploiter can always pass the name you expect.
I meant that it wasn’t a pre-declared variable, I thought I would point that out since it does kind of look like that.