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

Why is does the output say that new is not a valid member of modulescript?(SOLVED)

Hey guys I made a module script named Player:

local Inventory = require(game.ServerScriptService.PlayerData.Inventory.Inventory)

local Player = {}
Player.__index = Player

function Player.new()
	local NewPlayer = setmetatable({}, Player)

	NewPlayer.Inventory = nil
	NewPlayer.Stats = nil
	
	return NewPlayer
end

function Player:SetInventory(inventory)
	self.Inventory = inventory
end
function Player:GetInventory()
	return self.Inventory
end

function Player:SetStats(stats)
	self.Stats = stats
end
function Player:GetStats()
	return self.Stats
end

return Player

I required the module script in a script called Main:

local Players = script.Parent.Players
local Player = script.Parent.Player

local Stats = script.Parent.PlayerData.Stats
local Inventory = script.Parent.PlayerData.Inventory

function PlayerJoined(player)
	local ID = "player_"..player.UserId
	
	-- Creates a player and stores the info in the module script Players.
	local NewPlayer = Player.new()
	NewPlayer:SetStats(Stats)
	NewPlayer:SetInventory(Inventory)
	Players[ID] = NewPlayer
end

game.Players.PlayerAdded:Connect(PlayerJoined)

I was wondering why when I say this in the main script:

local NewPlayer = Player.new()

It gives me an error that says "new is not a valid member of modulescript". I've done this a thousand times so I was just wondering what I'm not seeing right now lol.

Thank you!

SOLUTION: I forgot to require it cause I'm dumb.

6 comments

0

Have you tried using :WaitForChild() ? Sometimes the server loads slower.

0

You can't use the require function in a module script

0

Yes you can @CreationNation1

0

You are not requiring it,you need to do this

local yourModule = require(path_to_module)
0

You are not requiring it,you need to do this

local yourModule = require(path_to_module)
0

You can, I'm just dumb and didn't require them. I just put the path for some reason lol.

1 answer

You forgot to require the ModuleScript on line 2 of your main script. Change local Player = script.Parent.Player to local Player = require(script.Parent:WaitForChild("Player")).

2 comments

0

oh lmao, mb didnt see u posted it before me

0

AH SHOOT I THOUGHT i DID. Lol thank you.

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