Attempt to index nil with 'new'

i want the script to be able to access the module script in order to gain data for the enemy, but it says the error in the title in relation to line 7

i can explain more if necessary

oh, and, the “enemie” is because i wasnt sure if the calling line was the issue, so i duplicated it and changed it to not be called upon (the one spelled correctly is the one CURRENTLY being used, but both have been tried)

Why did you use the Instance class at the beggining of Instance.FirstEnemy.new()?

not sure, it didnt work before, so i thought it may work then

actually, since ive changed it up a bit, imma see if deleting thatll fix it

yeah the error still appears once ive got rid of it, but ill make sure to keep it out for the rest of this

you need to return enemy1 in the function
(also don’t use Instance. like Cozmo said)

OHHHH I DIDNT DO THAT THATS PROBABLY WHY

will update if/once it works

okay so i think that was an issue, but it wasnt the main issue in this post

probably saved me some trouble later, so thanks for that atleast!!

Make sure you have correctly set up the new() function on your module correctly.
It should look something like this:

ModuleScript:

local enemies = {}

enemies.new = function(enemyName:string)
	print("Creating a new enemy named "..enemyName)
end

return enemies

Defining(enemyName:string) enemyName as a string wasn’t necessary but it makes your scripting experience easier when you are using a lot of unknown variables like this:
image

Server or LocalScript:

local enemies = require(game.ReplicatedStorage.FirstEnemy)

enemies.new("Builderman") --Prints; Creating a new enemy named Builderman

Also an irrevelant tip; you can store your ModuleScripts inside of ServerStorage or ReplicatedStorage for better organization as they are designed for that.
Store them in ServerStorage if you don’t want the client to access that ModuleScript, store them in ReplicatedStorage if you want both the server and the client to see it.

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