0

I'm trying to help my son with Roblox Studio. I'm an app developer but have never used Roblox before.

I'm stuck being able to call a function from another file...

We have a file called InsRankHandler under ServerScriptService. It has a Frame with some Labels and I can change the text in the labels just fine when using the game.Players.PlayerAdded:Connect callback.

We now want to change the labels text from an Adonis function.

I've added a function to the InsRankHandler file like this:

local InsRankHandler = {}
function InsRankHandler.setText(text)
    local clone = script.Rank:Clone()
    clone.Frame.Name1.Text = text
end
return InsRankHandler

Then I try to call it from the Adonis function like this:

local fun = require(game.ServerScriptService.InsRankHandler)
fun.setText("something");

but this gives me an error of an incorrect require.

If I print like this:

print(game.ServerScriptService.InsRankHandler);

it correctly shows InsRankHandler so I can't figure out what is wrong. I know it's probably some really obvious syntax error.

EDIT -----

I've added a ModuleScript under ServerScriptService that contains this:

local module = {}

function module.setText(text)
    print("setText:")
end

return module

Then in the Adonis function (that is also under ServerScriptService) I try to access it like this:

local fun = require(game.ServerScriptService.module)

or like this:

local fun2 = require(game.ServerScriptService.ModuleScript.module)

but both give me the error: module is not a valid member of...

Darren
  • 10,182
  • 20
  • 95
  • 162
  • It correctly shows the handler because it is the handler, no need to further require it :) – Luke100000 Aug 16 '23 at 05:26
  • If I try and call it directly like this `game.ServerScriptService.InsRankHandler.setText("test");` I get the error `setText is not a valid member of Script "ServerScriptService.InsRankHandler"` – Darren Aug 16 '23 at 08:27
  • Oh I'm sorry, misunderstood that. I read a bit more, and found [shared](https://stackoverflow.com/questions/1055534/how-to-call-functions-in-other-script-files-in-roblox) as a way to share functions but what you want is a [ModuleScript](https://create.roblox.com/docs/reference/engine/classes/ModuleScript). Maybe not relevant, but did you explicitly created a ModuleScript, e.g. as described here https://create.roblox.com/docs/education/coding-6/intro-to-module-scripts? Maybe it differentiate between Script and ModuleScript. – Luke100000 Aug 16 '23 at 08:42
  • Please see my edit above. – Darren Aug 16 '23 at 09:45
  • 1
    how did you name the file? Is it called `module`? – Luke100000 Aug 16 '23 at 11:10
  • The file is named `ModuleScript` then inside it I have the module called `module`. It can print `ServerScriptService.ModuleScript` but fails when trying to print `ServerScriptService.ModuleScript.module`. I'm wondering if maybe Adonis just doesn't have access to the modules. – Darren Aug 16 '23 at 11:26
  • I've created a brand new empty game. Added `adonis` and dragged it to `ServerScriptService`. Created a `ModuleScript` but the `adonis` function just can't seem to see the module inside the file. – Darren Aug 16 '23 at 11:59

0 Answers0