Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

ModuleScript not being accessed correctly?

Hi, I'm using a Module Script to store and send data to other parts of my game, so to make sure the system would work I set up a small test, which is proving to be more difficult than I thought. The below code is from the ModuleScript, which has a folder inside of it. Inside the folder are different types of values.

TL;DR read output

local library = {}
	
	function loadpokemon(PokeID)
		local pokefile = game.Workspace.Library:FindFirstChild(PokeID)
		local pokename = pokefile.PokemonName.Value
		local pokehealth = pokefile.Health.Value
		local stats = {pokename, pokehealth}
		
		print("Pokemon name: "..stats[1])
		print("Pokemon health: "..stats[2])
	end
	
return library

Now I have a normal Script that is attempting to use the function above. It sends the value 25, which is the ID I use to identify Pikachu.

local library = require(game.Workspace.Library)

library.loadpokemon(25)

The error I receive is:

Workspace.Script:5: attempt to call field 'loadpokemon' (a nil value)

Thanks for any help.

1 answer

You'll have to index the function into the library table.

local library = {}

    library.loadpokemon = function(PokeID)
        local pokefile = game.Workspace.Library:FindFirstChild(PokeID)
        local pokename = pokefile.PokemonName.Value
        local pokehealth = pokefile.Health.Value
        local stats = {pokename, pokehealth}

        print("Pokemon name: "..stats[1])
        print("Pokemon health: "..stats[2])
    end

return library

Just in case:

Tables

ModuleScripts

This discussion is preserved as it was. New answers, comments, and votes are closed.