Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 3 days ago
Edited 3 days ago

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.

0
Have you tried using :WaitForChild() ? Sometimes the server loads slower. LazerShatters 264 — 3d
0
You can't use the require function in a module script CreationNation1 329 — 3d
0
Yes you can @CreationNation1 Brandon1881 682 — 3d
0
You can, I'm just dumb and didn't require them. I just put the path for some reason lol. SethHeinzman 285 — 3d

2 answers

Log in to vote
2
Answered by 3 days ago

You are not requiring it,you need to do this

local yourModule = require(path_to_module)
0
Whoops thought I did lol. SethHeinzman 285 — 3d
Ad
Log in to vote
2
Answered by 3 days ago

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")).

0
oh lmao, mb didnt see u posted it before me CaioAlpaca 348 — 3d
0
AH SHOOT I THOUGHT i DID. Lol thank you. SethHeinzman 285 — 3d

Answer this question