How the :Disconnect function works?

Hello, I wanted to know how I use the function called “:Disconnect”
but I just can’t understand how it works,
I tried some methods
and even tried to look in the documentation
but nothing helped me
what I want to do is that the function is only activated when the player
is playing on that block
and when the player is not playing
in it the function is deactivated,
here are my code:

local ts = game:GetService("TweenService")

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)

	
	local g = game.ReplicatedStorage:WaitForChild("Candy")
	
	local gc = g:Clone()
	gc.Parent = game.Workspace
	gc.CFrame = plr.Character:GetPrimaryPartCFrame() * CFrame.new(0,-3,0)
	
	local twinfo = TweenInfo.new(10, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0,true,0)
	
	local tsPlay = ts:Create(gc,twinfo, { Size = Vector3.new(92.162, 3.91, 95.276)})
	tsPlay:Play()
	
	-----------------
	
	local plrSkill = plr.Name
	
	gc.Touched:Connect(function(hit)
		
		local player = hit.Parent
		local hum = player:FindFirstChild("Humanoid")
		
		if hit.Parent.Name == plrSkill then return end
			
		if hum  then
			hum.WalkSpeed = 2
			hum.Sit = true
		end
	end)

	
	
	
	script.Enabled = false
	task.wait(37)
	script.Enabled = true
	
end)
2 Likes

You can Use a :Disconnect event, and by Disconnecting it, you no longer allow Input from the RBXScriptSignal. For Example, I see you have a .Touched Event, so if we wanted to disconnect it after it being touched you can do something like:

local Connection
Connection = gc.Touched:Connect(function(hit)
		
		local player = hit.Parent
		local hum = player:FindFirstChild("Humanoid")
		
		if hit.Parent.Name == plrSkill then return end
			
		if hum  then
			hum.WalkSpeed = 2
			hum.Sit = true
            Connection:Disconnect()
		end
	end)

So what this would do, is when the touch it, if they have a humanoid and the plr skill, it fires once. If they try and restep on it, nothing happens, as the signal is disconnected in this example. Hope this helps!

2 Likes

I think you could use :Once and it’ll automatically disconnect it after it fires once, but yes that’s a good example but maybe you want to disconnect the event if the player has maybe 20 in game currency.

Not in this case. Once fires once, but if the first thing it touches doesn’t meet the condition of the if statement, then it doesn’t do the effect of changing the humanoid speed, and instead just a void and nothing happens and it won’t fire again

1 Like

Yeah but, that’s what I sort of meant when I said this:

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