#Maybe I'm missing something but why won't this work?

1 messages · Page 1 of 1 (latest)

echo remnant
#

btw i did not use ai, comment above .new() is for autocompletion, so i know what to put in args
module (oop-ish)

local Weapon = {}
Weapon.__index = Weapon
Weapon.weaponRegistry = {}

export type Weapon = typeof(setmetatable({} :: {
    template: Tool,
    name: string,
    damage: number,
    hitSpeed: number,
    speedModifier: number
}, Weapon))


--[[
Constructs a new instance of class Weapon

template: Dummy Tool to use as a base
name: Unique identifier for the weapon
damage: Damage dealt per hit
hitSpeed: Time (in seconds) between each hit
speedModifier: Walkspeed multiplier when held
]]--
function Weapon.new(cfg: {any}): Weapon
    local self = setmetatable({}, Weapon)

    self.template = cfg.template or error("arsenal.Weapon.new() -> templatel set")
    self.name = cfg.name or error("arsenal.Weapon.new() -> No name set")
    self.damage = cfg.damage or error("arsenal.Weapon.new() -> No damage set")
    self.hitSpeed = cfg.hitSpeed or error("arsenal.Weapon.new() -> No hitSpeed set")
    self.speedModifier = cfg.speedModifier or 1

    return self
end

function Weapon:init(): ()
    local tool: Tool = self.template:Clone()
    
    tool.Activated:Connect(function()
        print("activated")
    end)
    
    Weapon.weaponRegistry[self] = tool
end

return Weapon

server script that uses this (below)

local Weapon = require(game.ReplicatedStorage.Weapon)


game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        for _, kid in char:GetDescendants() do
            if kid:IsA("BasePart") then
                kid.CollisionGroup = "Player"
            end
        end
    end)
end)

Weapon.new({
    template = game.ServerStorage.Dummies.Katana,
    name = "Katana",
    damage = 7,
    hitSpeed = 0.3,
    speedModifier = 1.12
}):init()

for wep: Weapon.Weapon, tool: Tool in Weapon.weaponRegistry do
    tool:Clone().Parent = game.StarterPack
end
#

Maybe I'm missing something but why won't this work?

tender warren
#

you're putting the object table as an index in weaponRegistry?

echo remnant
#

key = instance of class weapon
value = tool with some events (unimplemented yet)

tender warren
#

what isnt working?

echo remnant
#

event

#

it wont fire

#

i forgot to mention in original post whats not working oops

tender warren
#

Did you playtest, grabbing the weapon and click to fire the event?

echo remnant
#

yes

#

equipped doesnt work too

#

(the event)

#

so i assume none work

serene brook
echo remnant
#

print() in .Activated doesnt pop up

serene brook
tender warren
#

You're cloning a new tool that isn't applied by the custom OOP you made.

tender warren
#

in loop

echo remnant
#

yeah i meant server script by that

tender warren
#

Yeh

echo remnant
#

still not working

#

maybe because the original tool is in the starterpack?

tender warren
#

just parent the tool you made instead of clone a new one

echo remnant
#

i did that

tender warren
#

qh

echo remnant
#

i mean i fixed it

serene brook
# echo remnant i parent it later

oh i see Weapon.weaponRegistry[self] = tool missed that part, but then you clone it again and parent it to the wrong thing.
event connections don't persist through cloning

for wep: Weapon.Weapon, tool: Tool in Weapon.weaponRegistry do
    tool:Clone().Parent = game.StarterPack
end```
echo remnant
#

hm

serene brook
#

and because you put it in starterpack, it clones for a 3rd time

echo remnant
#

wow

tender warren
#

yii

echo remnant
#

so how can i still have connections after it clones into inventory

#

brb

serene brook
#

that's one way

echo remnant
#

another question

serene brook
#

so like require(weaponregister) register:collect(self) end

#

or you could technically use collectionservice with the same pattern, it's just slightly different approach

echo remnant
#

i'd like to have some customization for a weapon, so some have special stuff to them, e.g. kill streak bonuses, special effects applied to player, etc

#

how would i implement such thing

serene brook
#

that's up to you fingerguns

#

lots of ways to make gun systems, i've made my fair share of them in other games

#

yet to make my first one in roblox

echo remnant
#

i think i wasnt clear, i meant how can i add inheritance?

#

im new to luau oop

serene brook
#

or just do it directly

#

myweaponbase:basefire() inside the weapon:fire()

#

no shame in that salute it's simple, crude, but extremely effective and very beginner-friendly compared to metatable inheritence if you never used metatables before.

echo remnant
serene brook
#

could have an array of different templates and use those as a base weapon, that works too 👍

serene brook
#

idk

echo remnant
#

im confused

serene brook
#

woops

echo remnant
#

if you mean different templates for different weapons i already do that

#

thanks for help

echo remnant
#

@serene brook forgot to ask one last thing: how would i reference self in a cloned (into the tool) script?

echo remnant
#

i meant weapon's stats

#

like self.damage

serene brook
#

or something like that, there's lots of ways to do this 👍

echo remnant
#

oh i planned to use registry as a way of storing non runtime weapons

#

indeed, i need to restructure a lot of stuff

serene brook
#

loots of ways to do that too coz why not remove the tool entirely and make your own system that is kind-of like roblox tools but done your way?

#

this ranges from very simple to very complex so... pick one :p

echo remnant
#

i tried to do that but custom tool system seems very hard

echo remnant
#

yeah