Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years 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. nekosiwifi 398 — 4y
0
You can't use the require function in a module script CreationNation1 459 — 4y
0
Yes you can @CreationNation1 Brandon1881 721 — 4y
0
You are not requiring it,you need to do this ~~~~~~~~~~~~~~~~~ local yourModule = require(path_to_module) ~~~~~~~~~~~~~~~~~ CaioAlpaca 342 — 4y
View all comments (2 more)
0
You are not requiring it,you need to do this ~~~~~~~~~~~~~~~~~ local yourModule = require(path_to_module) ~~~~~~~~~~~~~~~~~ CaioAlpaca 342 — 4y
0
You can, I'm just dumb and didn't require them. I just put the path for some reason lol. SethHeinzman 284 — 4y

1 answer

Log in to vote
2
Answered by 4 years 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 342 — 4y
0
AH SHOOT I THOUGHT i DID. Lol thank you. SethHeinzman 284 — 4y
Ad

Answer this question