Hey Dev Forum peeps, i was wondering if anyone possibly knew how id be able to call a function from another script? that probably sounds a bit odd but heres what i mean, i currently have two scripts, a local script with:
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Part = Workspace:WaitForChild("Infantry")
local function Called(Player)
if Player ~= LocalPlayer then return end
Part:Destroy()
end
I want the module script to call the “local function Called(Player)” thats in the local script when it executes the player function, but i currently aint got a clue how to do that, i was checking out the module script documents cause i heard that module scripts are able to do such a thing but im either blind or don’t understand enough because i didnt see anything that was worth while.
I also dont even know if this is possible or not cause the entire point of a module script is so you can make functions that can be called externally from outside the module script so clarification would be nice.
ModuleScripts are Dependent on other Scripts to function, so if you call it on the Client, it will run on the Client because of the Script, and you are able to access its contents.
If you want the function to be fired externally, look into BindableEvents or BindableFunctions, if you intend to fire it across the Server/Client Boundary, use a RemoteEvent or RemoteFunction
You got the whole idea backwards.
The purpose of module scripts is to call functions from it within your script
So to use your Action.Execute() function within your localscript
--localscript
local player = game.Players.LocalPlayer
local Action = require(somewhere.ModuleScript)
Action.Execute(player)
I mean if you really need to do this, you could just pass the function as an argument to the action.Execute function, but as arbi said there are better ways to go about doing it.
local function Called(Player)
if Player ~= LocalPlayer then return end
Part:Destroy()
end
module.Execute(player, Called)
Action.Execute = function(player, func)
func(player)
end
There’s really no benefit to doing this way, because you’re really just creating a function that executes other functions and it’s just overall messier.
Alright so would it look like somethling like this or have i specified “player” wrong or have i understood what i had to do incorrectly.
--Local script
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Part = Workspace:WaitForChild("Infantry")
local module = game.ServerScriptService.DialogueServerScript.Actions.After.AfterAction
local player = game.Players.LocalPlayer
local function Called(Player)
if Player ~= LocalPlayer then return end
Part:Destroy()
end
module.Execute(player, Called)
Ok uh, this may sound taboo and also might make the the thing imposibble (i dont know yet) but what if I only had (player) to work with and not (player, func) would i have to specify what func is in the action or would that not work at all. the reason why i only have (player) to work with is because the module script im using is not that good and doesnt allow other variables other than (player).
I mean if you really need to be using the structure where you pass a function to be called, yes, you would need to define func, but the best solution all around would be to just put whatever needs to be run inside of action.Execute itself.
I understand that, its just that for some reason if i add anything to (player) the script just stops working entirlery so i guess i just have to play by its rules for the moment.
Anyways if i were to define func what would i put to the define it?
Well if starterplayerscripts.the bruh is a module script then you need to require it, and then upon require if it returns a function, then yes that will work.
Alright so you’re trying to do something on the client when the Action.execute is run right?
So it depends if this function in the module script is being ran on the server or the client.
But lets say this module script function is being run on the server. (because were getting a player parameter, so I would assume this does something on the server to whatever player)
That means we have to go from the server to the client.
For that we can use a Remote Event.
Sooo to create a Remote Event you can just make one in your Explorer tab and I would put in the ReplicatedStorage.
Then in the Action.execute function you would put:
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- this is an example of a path to a remote event
RemoteEvent:FireClient(player)
Then in ANY localscript you would put:
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- use the same path as used on the server side
RemoteEvent.OnClientEvent:Connect(function()
-- your code that you want to do locally
end)