Function not disconnecting

Hello, I’m making a selection script right now, and I encountered a problem when trying to turn the script off I use the disconnect function and it doesn’t do anything I can still select anyone Here’s part of the script:

local selectMode = enableSelectMode.Event:Connect(function()
    local uisConnection = UIS.InputBegan:Connect(function(input, processed)
        if processed then return end
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            local raycast = mouseRaycast()
            if raycast and raycast.Instance then
                local humanoid = raycast.Instance.Parent:FindFirstChildOfClass("Humanoid")
                if humanoid and highlightedPlayer == nil then
                    local playerObject = game.Players:GetPlayerFromCharacter(humanoid.Parent)
                    if playerObject then
                        print("Selected player:", playerObject.Name)
                        highlightedPlayer = playerObject.Name
                        sendPlayerData:Fire(playerObject.Name)
                        CloneStuff(raycast.Instance.Parent)

                    end
                elseif not humanoid and highlightedPlayer ~= nil then
                    print("Deselecting: ".. highlightedPlayer)
                    RemoveStuff()
                elseif humanoid and highlightedPlayer ~= nil then
                    print("A Player is already selected")
                end
            end
        end
    end)
end)

deselectPlayer.Event:Connect(function()
    if highlightedPlayer ~= nil then
        RemoveStuff()
    end
    print("Disconnecting")
    selectMode:Disconnect()
end)

I hope someone can help

Are there any errors in the output?
Does the print("Disconnecting") in the deselect function print?
Can you show how you’re defining the variable for the deselect remote? If you’re not using WaitForChild, it could be that it isn’t loaded in when you define it and thus equals nil.
Is there any indication that RemoveStuff runs? Can you show the RemoveStuff function? Something in it might be forcing the thread to wait and therefore it never gets back to the deselect function to disconnect the connection.

The function does print Disconnecting
The variable is a BindableEvent and it is defined at the top and uses wait for child so it is defenently loaded

Like this (Sorry for the weird formating)

local deselectPlayer = RS.AscendedAdminReplicatedStorage:WaitForChild(“DeselectPlayer”)

Here’s the removeStuff function

local function RemoveStuff()
	print("Remove Stuff Called")
	local selectedPlayer = game.Players[highlightedPlayer]
	local selectedPlayerCharacter = selectedPlayer.Character
	for _,obj in selectedPlayerCharacter:GetChildren() do
		if obj:IsA("Highlight") then
			obj:Destroy()
		elseif obj.Name == "Head" then
			for _,v in obj:GetChildren() do
				if v:IsA("Attachment") then
					v:Destroy()
				elseif v:IsA("Beam") then
					v:Destroy()
				end
			end
		end
	end

Sorry for the late response I was out from home :grinning:

No problem!

I didn’t see this before, but it looks like you aren’t disconnecting the uisConnection in the enable function. selectMode is getting disconnected as intended, but uisConneciton isn’t and will still run when something is pressed. Try declaring it outside of the enable function and disconnecting it in the deselect function as well.

Okay so now the function disconnect nicely, now I have to figure out how to connect it
I tried putting it into a function so I can call but that doesn’t seem to work
Here’s the code:

local function mouseRaycast()
	local mousePos = UIS:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePos.X, mousePos.Y)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = game.Players:GetPlayers() -- Include all players
	params.FilterType = Enum.RaycastFilterType.Blacklist -- Use Blacklist to exclude certain objects if needed
	local result = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 100000, params)
	return result
end

function ShootRaycast()
	local raycast = mouseRaycast()
	if raycast and raycast.Instance then
		local humanoid = raycast.Instance.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid and highlightedPlayer == nil then
			local playerObject = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			if playerObject then
				print("Selected player:", playerObject.Name)
				highlightedPlayer = playerObject.Name
				sendPlayerData:Fire(playerObject.Name)
				CloneStuff(raycast.Instance.Parent)
			end
		elseif not humanoid and highlightedPlayer ~= nil then
			print("Deselecting: " .. highlightedPlayer)
			RemoveStuff()
		elseif humanoid and highlightedPlayer ~= nil then
			print("A Player is already selected")
		end
	end
end
local function UISMANAGER(val)
	uisConnection = UIS.InputBegan:Connect(function(input, processed)
		if processed then return end
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			ShootRaycast()
		end
	end)
end


local selectMode = enableSelectMode.Event:Connect(function()
	ShootRaycast()
	UISMANAGER()
end)

deselectPlayer.Event:Connect(function()
	if highlightedPlayer ~= nil then
		RemoveStuff()
	end
	print("Disconnecting")
	selectMode:Disconnect()
	uisConnection:Disconnect()
end)

From my testing, this runs the first time, but once you disable selectMode, it will stop listening for the enable bindable to be fired and won’t ever reconnect uisConnection.
Try the following:

local enabled = false
enableSelectMode.Event:Connect(function()
	if not enabled then
		enabled = true
		ShootRaycast()
		uisConnection = UIS.InputBegan:Connect(function(input, processed)
			if processed then return end
			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				ShootRaycast()
			end
		end)
	end
end)

deselectPlayer.Event:Connect(function()
	if highlightedPlayer ~= nil then
		RemoveStuff()
	end
	uisConnection:Disconnect()
	enabled = false
end)
1 Like

Worked like a charm, thanks dude :smile:

1 Like

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