My doubt when disconnecting events from functions

No problem. Good luck on whatever game you are gonna make!

Here is a demonstrative example that, when player touches the part, it dies and the event is disconnected:

local deathPart = game.Workspace.DeathPart

local function onTouch(hit)
    -- Check if the hit is a character's part
    local character = part.Parent
    if character and character:IsA("Model") and character:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(character)
        if player then
            character:FindFirstChildOfClass("Humanoid").Health = 0
            
            -- Disconnect the event listener when player dies
            deathPart.Touched:Disconnect()
        end
    end
end

-- Connect the function to the Touched event.
deathPart.Touched:Connect(onTouch)

Using :Once on this example can be also another way to reach this result:

-- Get reference to the part
local deathPart = game.Workspace.DeathPart

-- Function to handle player touching the part
local function onTouch(part)
    -- Check if the part is a character
    local character = part.Parent
    if character and character:IsA("Model") and character:FindFirstChild("Humanoid") then
        -- Get the player from the character
        local player = game.Players:GetPlayerFromCharacter(character)
        if player then
            -- Kill the player's character
            character:FindFirstChildOfClass("Humanoid").Health = 0
            
            -- Here, the :Disconnect() is not needed, as the player will die and it will run Once.
        end
    end
end

-- Connect the function to the Touched event of the death part
deathPart.Touched:Once(onTouch)

Yes he tried to :Disconnect the signal (.Touched and anything else with that lightning symbol are called RBXScriptSignals). That isnt possible and must have been a typo or something

Hey, last question, if i have connections inside a script, and i disable this script, connections are deleted or something?

I actually would not know cause i never tried that.

Oh it is ok no problem, thanks anyways!

Hello
I have Collection Script for openning windows, I have quite a lot in the scene
There is also a memory leak ??

local CollectionService = game:GetService("CollectionService")

local TweenService = game:GetService("TweenService")



for i, windows in pairs(CollectionService:GetTagged("Windows")) do
	
local ProximityPrompts = windows["Proximity Prompts"]
local Settings = windows.Settings
local Proxi = windows["Prompt Holder"].ProximityPrompt

local doopen = workspace.Sound.dooropen
local doclose = workspace.Sound.doorclose

-- Variables --
local proximityPrompts = {}
local connections = {}

local movingPart = Settings["Moving part"].Value
local part1 = Settings["Position 1"].Value
local cframe1 = part1.CFrame
local part2 = Settings["Position 2"].Value
local cframe2 = part2.CFrame

local tweenDuration = Settings["Time"].Value
local removePositionParts = Settings["Remove position parts"].Value

local Advanced = Settings.Advanced
local easingStyle = Advanced.EasingStyle.Value
local easingDirection = Advanced.EasingDirection.Value

local isCframe1

local tween

-- Funkce --
local function setup()
	movingPart.CFrame = cframe1
	isCframe1 = true

	if removePositionParts then
		part1:Destroy()
		part2:Destroy()
	end
end

local function getProximityPrompts()
	for index, child in pairs(ProximityPrompts:GetChildren()) do
		if child:IsA("ObjectValue") then
			local prompt = child.Value
			if prompt:IsA("ProximityPrompt") then
				table.insert(proximityPrompts, prompt)
			end
		end
	end
end

local function tweenMovingPartTo(cframe)
	if tween then
		tween:Cancel()
		tween:Destroy()
	end
	local tweenInfo = TweenInfo.new(tweenDuration, easingStyle, easingDirection)
	tween = TweenService:Create(movingPart, tweenInfo, {CFrame = cframe})
	tween:Play()
end

local function onTriggered(player)
	if isCframe1 then
		print("isCframe1 = false")
		isCframe1 = false
		tweenMovingPartTo(cframe2)
		Proxi.Enabled = false
		doopen.Playing = true
		wait(2)
		Proxi.ActionText = "CLOSE"
		Proxi.Enabled = true		
	else
		isCframe1 = true
		tweenMovingPartTo(cframe1)
		Proxi.Enabled = false
		doopen.Playing = true
		wait(2)
		Proxi.ActionText = "OPEN"
		Proxi.Enabled = true
	end
end


setup()
getProximityPrompts()
	for index, prompt in ipairs(proximityPrompts) do
		local connection = prompt.Triggered:Connect(onTriggered)
		table.insert(connections, connection)
	end
end

video example:

and if yes, how would you solve it

Thanks in advance for ideas
Codycheck

local deathPart = game.Workspace.DeathPart

local function onTouch(hit)
    -- Check if the hit is a character's part
    local character = part.Parent
    if character and character:IsA("Model") and character:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(character)
        if player then
            character:FindFirstChildOfClass("Humanoid").Health = 0
        end
    end
end

-- Connect the function to the Touched event.
local connection = deathPart.Touched:Connect(onTouch)

connection:Disconnect()

typo, sorru

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