ClickDetector inside RemoteEvent firing other RemoteEvent's ClickDetector

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

  1. What do you want to achieve? Prevent yes.mouseclick from being fired from another remote event

  2. What is the issue? yes.mouseclick seems to be able to fire outside of the remote event. As shown here, despite the viewpoint handler being under the key remote event, it still runs the code when firing the dresser event. The key is not supposed to show by simply pressing yes on the drawer. This only happens when I pick up the key first.

  3. What solutions have you tried so far? Adding returns, if this then, lots of random stuff

Below is the code:

local Players = game:GetService("Players")
local re = game:GetService("ReplicatedStorage")

--VIEWPORTS--
local viewFrame = script.Parent.Parent:WaitForChild("ViewportFrame")
local bKeySpin = viewFrame.KeySpin

--VARIABLES--
local cam = workspace.CurrentCamera

local yesType = "Yes"
local noType = "No"

local typingSpeed = 0.025

local frame = script.Parent
local yes = frame.Yes
local no = frame.No

local whatIsIt = frame.WhatIsIt
local search = frame.Search
local exit = script.Parent.Parent:WaitForChild("Exit")

--FUNCTIONS--
local function resetcam()
	cam.CameraType = Enum.CameraType.Custom	
end

local function typeText(guiElement, text)
	for i = 1, #text do
		guiElement.Text = string.sub(text, 1, i)
		wait(typingSpeed)
	end
end

local function resetText()
	whatIsIt.Text = ""
	search.Text = ""
	yes.Text = ""
	no.Text = ""
	frame.Visible = false
end

--DRAWER--
re.DresserRE.OnClientEvent:Connect(function(dressermodel)
	local whatIsType = "A wooden dresser."
	local searchType = "Should I search it?"

	task.wait()
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CFrame = dressermodel.Cam.CFrame

	local dresser = dressermodel.Dresser

	frame.Visible = true

	typeText(whatIsIt, whatIsType)

	task.wait(0.15)
	typeText(search, searchType)
	typeText(yes, yesType)
	typeText(no, noType)

	yes.MouseButton1Click:Connect(function()
			resetText()

			exit.Visible = true

			for i, drawer in pairs(dressermodel.Drawers:GetChildren()) do
				drawer.ClickDetector.MaxActivationDistance = 10
			end

			dresser.ClickDetector.MaxActivationDistance = 0
			return
	end)

	no.MouseButton1Click:Connect(function()
		resetText()
		resetcam()
	end)

	exit.MouseButton1Click:Connect(function()
		resetcam()

		for i, drawer in pairs(dressermodel.Drawers:GetChildren()) do
			drawer.ClickDetector.MaxActivationDistance = 0
		end

		dresser.ClickDetector.MaxActivationDistance = 10

		exit.Visible = false
	end)
end)

--KEY--
re.KeyRE.OnClientEvent:Connect(function(key)
	local whatIsType = "A key."
	local searchType = "Should I pick it up?"

	task.wait()
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CFrame = key.Cam.CFrame

	frame.Visible = true

	typeText(whatIsIt, whatIsType)

	task.wait(0.15)
	typeText(search, searchType)
	typeText(yes, yesType)
	typeText(no, noType)

	yes.MouseButton1Click:Connect(function()
		resetText()

		resetcam()
		viewFrame.Visible = true
		bKeySpin.Transparency = 0

		local keySound = key.KeySound
		keySound:Play()

		local playerList = Players:GetPlayers()
		for _, player in ipairs(playerList) do
			re.DestroyEvents.DestroyKey:FireServer(player)
		end

		task.wait(3)
		viewFrame.Visible = false
		bKeySpin.Transparency = 1
	end)
	
	no.MouseButton1Click:Connect(function()
		resetText()
		resetcam()

	end)
end)

You never disconnect the connections you create within each RemoteEvent. That is what is causing your issue. If you wanted to fix it, you could just use :Once() instead of :Connect(). Once automatically disconnects the connection for you.

yes.MouseButton1Click:Once(function()
    -- Rest of code
end)
1 Like

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