#Error trying to call function in table

131 messages · Page 1 of 1 (latest)

kind river
#

Function where i call it:

local function onAbilityEvent(player, mousePos, a: typeof(AbilityClass))
    a:UseAbility(player)
end

base class:

local Ability = {}

function Ability.new()
    local ReturningData = {}
    ReturningData.Name = "AbilityBase"
    ReturningData.Damage = 50
    ReturningData.Cooldown = 2.5
    function ReturningData:UseAbility(player)
        print("Ability used ", ReturningData.Name, " by ", player)
    end

    return ReturningData
end

return Ability

child class:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AbilityClass = require(ReplicatedStorage.AbilityType)

local A = AbilityClass.new()

A.Name = "Blah Blah"
A.Cooldown = 2
A.Damage = 50
function A:UseAbility(player)
    print(player)
end

return A
hallow vine
#

do where is it erroring

#

so

#

you need to show the whole script

#

@bleak basin someone who's doing it without metatables

#

although incorrectly

hallow vine
kind river
torpid raft
#

hey can some1 help me? i have an error i cant even fix, its below this post

hallow vine
#

so show the whole first script

torpid raft
hallow vine
#

it's really rude shura

kind river
# hallow vine so show the whole first script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AbilityClass = require(ReplicatedStorage.AbilityType)

local abilityEvent = Instance.new("RemoteEvent")
abilityEvent.Name = "AbilityEvent"
abilityEvent.Parent = ReplicatedStorage

local function onAbilityEvent(player, mousePos, a: typeof(AbilityClass))
    a:UseAbility(player)
end

abilityEvent.OnServerEvent:Connect(onAbilityEvent)
torpid raft
#

no need to be rude

kind river
#

at "a:UseAbility(player)"

hallow vine
#

I'm not rude, you are

#

alright

torpid raft
#

IM NOT BEING RUDE, IM JUST F*CKING ASKING FOR HELP, IN WHAT WAY IS THAT RUDE

#

JEEZ

hallow vine
#

you can't pass functions in a remotevent

hallow vine
torpid raft
#

YOU ARE FIRST BEING RUDE

#

SO DONT BLAME ME

hallow vine
#

the next thing you say will make me report you

#

so you better stop

torpid raft
#

ASKING FOR HELP IS NOT BEING RUDE.

kind river
hallow vine
#

nope

#

however you can pass the information and using that instantiate one on the server

hallow vine
#

you've entered our conversation to spam it

torpid raft
hallow vine
#

that's extremely rude

torpid raft
#

I ONLY ASKED ONCE BC I DIDNT FIND ANY HELP

hallow vine
#

you really want to get banned?

#

you better shut up and leave this thread now

brave flareBOT
#

studio** You are now Level 2! **studio

torpid raft
#

NAH U ARE NOW THE RUDE ONE

#

THIS IS EVEN MORE NONSENSE THAT HOW MY PROBLEM IS HAPPENING

hallow vine
#

it just makes things unnecessarily complicated

kind river
#

any other recommended way i should do it?

hallow vine
#

yes by doing what I said

#

but you won't be able to understand that until you are aware of what you are doing

kind river
#

the thing is i know how to do it in other languages, im just new in lua and stuff

#

how can i get a better understanding of it?

hallow vine
kind river
#

i was thinking about using a compiler but i have no idea how to set it up and id prob still get stuck at some point

#

but ill check that out ig

hallow vine
#

a compiler?

kind river
#

yea to use a different language like java or smt

hallow vine
#

the lua reference sums up everything about lua perfectly

#

hell no

#

in roblox you use luau

#

not java or anything

kind river
#

alr

kind river
kind river
#

oh wait

#

i might have an idea

#

nope nvm

#

would the way be having the functions in the same file as the event?

bleak basin
bleak basin
#

you would have to reconsile both tables for that to be possible

#

or have your child class use ability function = the parent class one BUT you loose self there

#

aside from that, I discourage you to use inheritance, just compose

bleak basin
hallow vine
#

especially not from client to server lol

bleak basin
#

cause great you have inheritance, but you have no way to force the interface because:

  • A: there are no interfaces in lua
  • B: there are no private fields in tables
  • C: you cannot lock the table away w/o loosing the ability to modify it's members
#

then you have metaprogramming, but that's just optional

hallow vine
bleak basin
#

you just pass the minimum data needed to the client so he can either:
A: do something
OR IF YOU REALLY REALLY WANT THE OBJECT
B: Reconstruct it client sided

kind river
brave flareBOT
#

studio** You are now Level 3! **studio

bleak basin
#

ever heard of composition over inheritance?

kind river
#

i have not

bleak basin
#

instead of having IsA relations (inheritance)

#

you have HasA relations via composition

#

thiis can be simply done by constructing your other classes within the base class

#

i.e

#
local classA = require
local classB = require

local classC = {}

classC.new = function()
  local self = {}
  self.A = ClassA.new()
  self.B = ClassB.new()
  
  -- C data
  -- C methods

  return self

end
#

or how I prefer it

#

passing them as arguments

#
local classA = require
local classB = require

local classC = {}

classC.new = function(A, B)
  local self = {}
  self.A = A.new()
  self.B = B.new()
  
  -- C data
  -- C methods

  return self

end
#

were you have both a toggle and a slider

#

with inheritance you would probably go nuts trying to accomate 2 very different things, a toggle and a slider together into the same class

#

via composition, you don't, cause they just become ad-hoc components

#

in my case the slider is class

#

and the toggle is another class

#

here are they

#

now if for some reason I wanted to treat both a toggle and a slider as a single unit

#

instead of two different ones

#

I can just make anothe class, call it ToggleSliderClass

#

and it's constructor takes both a toggle and a slider

#
local ToggleSliderClass = {}
ToggleSliderClass.new = function(toggle, slider, config)
  -- class logic
end
kind river
#

so simply put, as u said, i have to make the ability base class and the abilities the same class

bleak basin
#

not necesarilly

#

they can be 2 separate entities

#

is my toggle and the slider the same class?

#

no

#

they are 2 different things

#

IF I WANTED to treat them as one

#

I would make a third class to manipulate them as a single unit

#

sure with it's own set of functions if that was useful for me

kind river
#

i dont totally get it tbh prob not smart enough, but ig imma try something

bleak basin
#

start with a simple class first

#

I think you jumped straight into inheritance w/o knowing how to make a class in lua

kind river
#

yea i dont rlly know much about lua

kind river
#

also whats the difference here?
in your script you use stuff like

function module:GetDisabledSliders()
    return States.Disabled
end

but also use

module.GetStates = function()
    return States
end
#

OHHH

#

i get it (what im supposed to do)

#

u make the different functions (dont have to be inside the abiltiy class) with an argument = The ability class, and then u pass through the data of the ability

#

i hope thats it

hallow vine
#

the difference between function table.name functionname() and table.functionname = function() is only syntactic

#

the difference between . and : is that for : the table it is called on is implicitly passed as self

#

you should learn basic lua

#

your trial and error approach doesn't seem very efficient

#

just read the documentation

kind river
#

tired rn ill see another day