Alright so im remaking the datastores and adding a class using metatables (followed a tutorial)
but i needed to add a folder (similar to leaderstats) to the player and i think i need to add it through the constructor
Don’t use long-polling like this: repeat task.wait() until plr.Character ~= nil
Instead do this: local char = plr.Character or plr.CharacterAdded:Wait()
I’m unsure why your constructor has the 2nd argument as “self”, if you do: local playerClass = playerclass:new()
It will automatically populate the self variable for you. Though in your case you should be fine with just doing: local self = setmetatable({},playerclass)
I would also move your leaderstats creation into its own method in the class and then have the constructor call it.
Sorry for the late response and also sorry that im pretty bad at scripting ngl because i dont know what you mean :{
Also apparently it works for my friend who joined my game?? makes no sense.
What i meant by “it doesnt work” is that the folder “Items” would not create
Are you 100% sure you executed @CE0_OfTrolling 's solution successfully? You mentioned you only did it in the constructor.
Here’s an explanation (more in-depth) of how it works:
The dot operator will not pass the item itself as a parameter, however the colon operator does.
So:
PlayerClass.SetItem(PlayerClass, player)
--is the same as
PlayerClass:SetItem(player)
When using colon notation, the first parameter passed is always stored in self. If you call with colon notation, the item itself is also passed as a parameter. Here’s some more information about that.
Since you only pass the player as a parameter, it is stored in the self parameter and the plr parameter is left as nil. Since it’s left as nil, when you assign the parent to what you think is the player, you’re actually assigning it to nothing.
You should change all instances of PlayerClass.SetItems(plr) to PlayerClass:SetItems(plr) throughout your code.
Sorry for late late reply but this didnt work either
ServerScriptService.Main.Server.PlayerClass:10: attempt to index nil with ‘Parent’ is the error
and this is my code
local playerclass = {};
playerclass.__index = playerclass;
function playerclass:SetItems(plrclass,plr)
local items = Instance.new("Folder")
items.Parent = plr
items.Name = "Items"
local slot1, slot2, slot3 = Instance.new("StringValue")
slot1.Parent = items
slot2.Parent = items
slot3.Parent = items
slot1.Name = "Primary"
slot2.Name = "Secondary"
slot3.Name = "Melee"
slot1.Value = "Winchester Model 70"
slot2.Value = "M1911"
slot3.Value = "Combat Knife"
end
function playerclass.new(plr)
local self = setmetatable({}, playerclass)
self.name = plr.Name
self.displayname = plr.DisplayName
self.character = plr.Character
self.cash = 0
self.level = 1
self:SetItems(plr)
return self
end
return playerclass