Disconnect a Fastcast function

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I wanted to know how to disconnect a function from fastcast
2 Likes

What do you mean by function? If you mean events such as LengthChanged or RayHit, treat them like how you would treat disconnecting a RBXScriptSignal (with a few exceptions).

local FC = require(script.FastCastRedux)
local cast = FC.new()

local connection = cast.RayHit:Connect(function()
	print("Hit!")
end)

connection:Disconnect() -- disconnect the event
1 Like

How about use the function like :Once() ?

1 Like

Interestingly, FastCast uses a module for it’s signals (FastCastRedux > Signal), you can see it in this line for .new()

function FastCast.new()
	return setmetatable({
		LengthChanged = Signal.new("LengthChanged"),
		RayHit = Signal.new("RayHit"),
		RayPierced = Signal.new("RayPierced"),
		CastTerminating = Signal.new("CastTerminating"),
		WorldRoot = workspace
	}, FastCast)
end

…and looking at the module, it only seems to have the methods :Connect(), :Fire(), :Wait(), and :Disconnect() – no :Once().

But I think you could implement your own code for that.

Also I edited something in the og post and apparently it took away the solution check… could you put it back on my original reply? I’ll try adding a :Once() for you – if I can figure out how this works.

1 Like

Thanks for your Support i’m so grateful for your suggestions
I have found the way to disconnect it at once

func = Caster.RayHit:Connect(function()
        func:Disconnect()
end)

I finally figured it out – if you ever want to just use :Once(), insert this line in FastCastRedux > Signal

function SignalStatic:Once(func)
	assert(getmetatable(self) == SignalStatic, ERR_NOT_INSTANCE:format("Once", "Signal.new()"))
	
	local connection = NewConnection(self, function() end)
	connection.Index = #self.Connections + 1
	table.insert(self.Connections, connection.Index, connection)
	
	connection.Delegate = function()
		func()
		connection:Disconnect()
	end
	return connection
end

and you could just use it like:

local FC = require(script.FastCastRedux)
local cast = FC.new()

local connection = cast.RayHit:Once(function()
	print("Hit!")
end)

I tested it and I believe it works, but caution is advised.

2 Likes

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