How would I call a local script function using a module script

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

and a module script with:

Action.Execute = function(player)
	
	--insert blah blah blah code or smt
	
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.

Thanks in advanced.

5 Likes

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

1 Like

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)

2 Likes

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.

3 Likes

Sorry for probably asking a bit of a simple question but is

Supposed to come back as an unknown global or was i supposed to specifty what it was using local before the function first.

2 Likes

Define it before calling module.Execute, yeah

1 Like

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)
---Module script
Action.Execute = function(player, func) 
	func(player)

	




end;
1 Like

That looks like it should work. You need to require your module though.

1 Like

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). :frowning:

1 Like

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.

1 Like

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?

1 Like

It would just be the function you are going to call

1 Like

by just straight up defining where it is like this

local func = game.StarterPlayer.StarterPlayerScripts["the bruh"]
func(player)

or do i do something else?

1 Like

woops qouted myself sorry bout that

1 Like

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.

1 Like

so um, we may have been thinking seperate things this entire time, so the bruh is actually the local script in this case.

1 Like

You can’t get anything from the source of a local script from another script.

1 Like

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.