Object copying issues

Help D:

Working on my skill system I noticed it wasn’t working OK.
Been 2 hours trying to get it right D:

My skill system works using skill objects.
The idea is that a skill object can define its own function when its updated and leveled up.

Skill class constructor:
[lua]
function newSkill(str)

newskill = {
	name=str,
	level=1,
	exp=0,
	owner=nil
}

newskill.setOwner = function(newowner)
	Msg("Setting new owner: "..newskill.name.. " " .. newowner:Nick() .."

")
newskill.owner = newowner
Msg("Owner set: “… newskill.owner:Nick() …”
")
end

return newskill

end
[/lua]

However, I need to be able to copy skills in order to give copies of them (and not the originals) to players.
I’m trying to have each specific skill as a children of the class “Skill” by doing something like:

[lua]
local skill = newSkill(“Wisdom”)
/*
Increases the player’s skill slots
*/
skill.levelup = function()
if skill.owner != nil then
skill.owner:ChatPrint("Your Wisdom level increased to "…skill.level)
skill.owner.skillslots = skill.owner.skillslots+1
skill.owner:ChatPrint(“You can now learn one extra skill”)
end
end
[/lua]
which creates a new skill with the name Wisdom and a specifically tailored levelup function.

Then this skill is added to the InitialSkill table, which holds all the skills given to players upon joining the game

Once a player joins, all the skills in the InitialSkill table are copied over to the player’s personal skillset.

I’m using this function to create deep copies of the skills:
[lua]
function CopySkill(skill)
if type(skill) ~= ‘table’ then return skill end
local mt = getmetatable(skill)
local res = {}
for k,v in pairs(skill) do
if type(v) == ‘table’ then
v = deepcopy(v)
end
res[k] = v
end
setmetatable(res,mt)
return res
end
end
[/lua]
Which works perfectly with 1 skill.
However, whenever I define additional skills, the copies of other skills acquire the functions of the skill that was defined last.

For example, if I define a skill named “Strength”, which its skill.levelup prints “Hello”, and a skill named “Intelligence” after it, which’s skill.levelup prints “Bye”, both originals work as intended, but all the copies of Strength print “Bye” on levelup. (As if they acquired the function from the Intelligence skill)

EDIT: Some extra bit of info:
This is the function for setting the initial skills
[lua]
function SetInitialSkillset(ply)
for k,v in pairs(InitialSkills) do
Msg("Copying skill: “…v.name…”
")

	local newskill = CopySkill(v)
	Msg("Copied: "..newskill.name.."

“)
Msg(newskill.setOwner…”
")
newskill.setOwner(ply)
table.insert(ply.skillset,newskill)
ply.skillslots = ply.skillslots+1
end
end
[/lua]
And this is what happens:

Notice that when setting the owner of Strength, it prints “Wisdom” as the name of the skill its setting the owner to.

Oh well, Since nobody seems to know how to fix this, I’ll just recode it from the ground up. I’ll try using the class() function described here

EDIT: I think I got it right now, but can’t test it, steam went offline, and the stoopid proxy in my current location doesn’t let it even get to Offline Mode…
Can’t even use a loader to get it to run because none have been working with the latest versions of GMOD :x (Good job there, garry :smiley: [That wasn’t sarcasm, jic])