Looked at other posts and ion really get it
i have this code
local BulletFolder = game.ReplicatedStorage.Bullets
local NormalBullet = BulletFolder.NormalBullet
local GunInformation = {
["Glock_Test"] = {
["Distance"] = 300,
["Damage"] = 12,
["MagSize"] = 14,
["UsedBullet"] = NormalBullet
},
["Ak47_Test"] = {
["Distance"] = 450,
["Damage"] = 24,
["MagSize"] = 30,
["UsedBullet"] = NormalBullet
}
}
GunInformation.GetInformation = function(Gun)
if GunInformation[Gun.Name] then
return GunInformation[Gun.Name]
else
return nil
end
end
GunInformation.__index = GunInformation
return GunInformation
and i want to make it so instead of this
GunInformation.GetInformation = function(Gun)
if GunInformation[Gun.Name] then
return GunInformation[Gun.Name]
else
return nil
end
end
its just this
GunInformation.GetInformation = function(Gun)
return GunInformation[Gun.Name]
end
and have it so if its not a vaild “gun” then it will default to “Glock_Test”
no clue how to use __newindex for this as i OOP is still very confusing to me
local meta = {
__newindex = function(self, index, value)
self[index] = value * 2
end
}
local tb = setmetatable({}, meta)
tb[1] = 1
print(tb[1]) -- > 2
__newindex is a function that can possibly modify a modification or a newly inserted value. In this case, we will set the index normally but multiply the value times to, hence the value * 2. When setting the index to 1 and the value to 1, the __newindex metamethod will interpret that before it’s actually set and will change the value, multiplying it by 2 before actually creating the new index.
In this case, you actually don’t want to use __newindex and you just want to use __index. This method can either be a separate table or function to return an index.
For your case, if you want to implement a default weapon, set this as your __index metamethod:
function(object, key)
return GunInformation.Glock_Test
end
but of course, you can just use an or operator:
local gun = GunInformation[Gun.Name] or GunInformation.Glock_Test
OOP or Object Oriented Programming is a type of programming that allows you to focus methods and variables around objects. Here’s an example of how to create an OOP car:
local manufacture = {}
function manufacture.new(carModel, carColor, carYear)
local car = {
Year = carYear,
Model = carModel,
Color = carColor
}
return car
end
return manufacture
This OOP car function does not contain any methods. Your code does not contain methods or objects that are created, so it is not OOP yet. I recommend researching OOP before you continue. though it might be a bit of a handful
i’ve looked at many tutorials, explainations, ect.
None of it goes over the advanced stuff i see in big module scripts (it may of actually gone over some of it, but didnt go too deep into explaination about it) and stuff, plus with what ive seen from these said tutorials it doesn’t really seem too helpful
(noted that ive only seen __index used to do stuff like)
Module1
local Tr = {}
function Tr.new(Letter)
local mtabel = setmetatable({}, Tr)
mtabel["ChosenLetter"] = Letter
return mtabel
end
function Tr:AddLetter(Letter)
Tr["AddedLetter#" .. #Tr - 1] = Letter
end
Tr.__index = Tr
return Tr
Module2
local Fr = {}
function Fr.ChosenNumber(Number)
local mTable = setmetatable({}, Fr)
mTable["ChosenNumber"] = Number
return mTable
end
Fr.__index = require(game.ReplicatedStorage.TrModule)
return Fr
tbh not even sure if i got those correct or not.
too lazy to do the server script forgot to add