Ideas on how to make a Server Event Listener

image

I added more functionalities but yeah still warns me

1 Like

Do you think you could send the script? I’d like to have a go at debugging your code myself.

function CustomEvent.new(Bindable_Event_Name: string): BindableEvent
	local self = {}
	setmetatable(self, CustomEvent)
	CustomEvent.Events[Bindable_Event_Name] = self
	
	self.Callbacks = {}
	self.Fired = false 

	return self 
end



function CustomEvent:WaitForEvent(BindableEvent: string): BindableEvent
	local Signal: BindableEvent
	local attempt = 0 

	repeat
		Signal = self.Events[BindableEvent] 
		attempt += 1
		task.wait(1)

		if attempt == 5 then 
			warn("Yielded for Event: "..BindableEvent.." for 5+ ".."seconds")
		end
	until
	Signal or attempt == 60

	return Signal
end



function CustomEvent:Connect(Callback_Name: string,callback: (any?) -> (nil)): nil 
	self.Callbacks[Callback_Name] = callback 
	return nil
end


function CustomEvent:DisconnectCallback(Bindable_Event_Name: string?,Callback_Name: string): nil
	CustomEvent.Events[self or Bindable_Event_Name][Callback_Name] = nil
	return nil
end


function CustomEvent:DisconnectAllCallbacks(): nil
	table.clear(self.Callbacks)
	return nil
end



function CustomEvent:Fire(...:any?): nil
	local Arguments = {...} -->> Stores all Arguments inside a Table.
	
	
	for _, callback: (any?) -> (nil) in next, self.Callbacks, nil do

		task.spawn(callback, table.unpack(Arguments)) 
	end
	return nil
end



export type BindableEvent = {
	["Connect"]: (self: BindableEvent, Callback_Name: string, any?) -> (nil), 
	["DisconnectCallback"]: (self: BindableEvent, Bindable_Event_Name: string?, Callback_Name: string) -> (nil),
	["DisconnectAllCallbacks"]: (self: BindableEvent) -> (nil),
	["Fire"]: (self: BindableEvent, any?) -> (nil), 
	["Callbacks"]: {(any?) -> (nil)}, 
	["Fired"]: boolean 
}

This is my Events Class Module

--!strict

local CustomEvent = {}
CustomEvent.Events = {}

CustomEvent.__index = CustomEvent

function CustomEvent.new(eventName: string): CustomBindable
	local self = setmetatable({}, CustomEvent)
	
	CustomEvent.Events[eventName] = self

	self.Callbacks = {}
	self.Fired = false

	return self
end



function CustomEvent:WaitForEvent(name: string): CustomBindable?
	local Signal: CustomBindable
	local attempt = 0 

	repeat
		Signal = self.Events[name] 
		attempt += 1
		task.wait(1)

		if attempt == 5 then 
			warn("Yielded for Event: "..name.." for 5+ ".."seconds")
		end
	until
		Signal or attempt == 60

	return Signal
end



function CustomEvent:Connect(callbackName: string, callback: (any?) -> (nil)): nil 
	self.Callbacks[callbackName] = callback 
	
	return nil
end


function CustomEvent:DisconnectCallback(eventName: string?, callback: string): nil
	CustomEvent.Events[eventName or self][callback] = nil
	
	return nil
end


function CustomEvent:DisconnectAllCallbacks(): nil
	table.clear(self.Callbacks)
	
	return nil
end



function CustomEvent:Fire(...:any?): nil
	local arguments: {any?} = {...}


	for _, callback: (any?) -> (nil) in next, self.Callbacks, nil do
		local asyncCallback = coroutine.wrap(callback)
		asyncCallback(table.unpack(arguments))
	end
	
	return nil
end



export type CustomBindable = {
	["Connect"]: (self: CustomBindable, callbackName: string, any?) -> (nil), 
	["DisconnectCallback"]: (self: CustomBindable, name: string?, callbackName: string) -> (nil),
	["DisconnectAllCallbacks"]: (self: CustomBindable) -> (nil),
	["Fire"]: (self: CustomBindable, any?) -> (nil), 
	["Callbacks"]: {(any?) -> (nil)}, 
	["Fired"]: boolean,
	["__index"]: CustomEvent,
	
	--you must add unused things that were inherited from the class
	["new"]: (eventName: string) -> (CustomBindable),
	["WaitForEvent"]: (CustomEvent) -> (CustomBindable?),
	["Events"]: {CustomBindable}	
}

All but one of the errors should be fixed; this error is in the .new function saying it cannot be casted to the type. This is because the metatable does not become a part of the actual table, I’ll leave you to figure out a workaround for this instead.

1 Like

Oh ok, so i just gotta reference all the values/ functions there?

Pretty much, but you might need to further modify your class to account for mock-OOP using metatables.

what do you mean? What is a mock-OOP?

image

Do i have to put these variables at the bottom too?

What i have right now

Like I briefly mentioned before, this system uses an Object-Oriented structure. Each bindable is an object, and they all inherit everything from the class. Luau does not support OOP fully, but you can achieve a basically identical effect using metatables, which we use. This is often called mock-OOP, because it’s mimicking Object-Oriented Programming’s effect.

Those variables you would need to put in the class along with any other variables or constants used within the class. You’ll know if it’s right because the strict warning will go.

i kind of understood the last bit of your post, imma see what i can do.
And respond back

1 Like

image
image

lol

bro i canno’t get this to work for the life of me

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.