Why does InputBegan happens multiple times even though I am triggering it once?

Hi! I was wondering why does the thing in the title happens and how can I make it only trigger a singular time while still keeping both functions in the LocalScript?

I’ll send both of my test scripts I made:

game.ReplicatedStorage.Events.ChatEvent.OnClientEvent:Connect(function()
	game:GetService("UserInputService").InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.E then
			print("mmonday left me broken")
		end	
	end)
end)

(LocalScript in StarterPlayerScripts)

script.Parent.Triggered:Connect(function(player)
	game.ReplicatedStorage.Events.ChatEvent:FireClient(player)
end)

(Script in a ProximityPrompt)

Output (I triggered the proximity prompt 10 times only)

I tried to disconnect the function already, didn’t work.
I’m sorry for any mistakes in my script, I’m a bit stressed out from trying to still solve the problem from my last topic.

1 Like

First off I highly recommend you don’t nest the InputBegan inside the ClientEvent and actually I believe that is what is causing your issue.

I believe that the Triggered event is being called multiple times which is causing the InputBegan to have multiple instances (causing the function to repeat 16 times).

My recommendation is that inside the LocalScript you add a boolean value (view code below) then just check it whenever the event is called.

local ChatEventCalled = false

userInputService.InputBegabn(connect(function()
if ChatEventCalled == false then return end

if input.KeyCode == Enum.KeyCode.E then
print("monday left me broken")
end
end)

Just as a comment, I highly recommend trying your best not to nest in general, here is a good video on it if you are interested in progressing in programming.

You could try the following:

game.ReplicatedStorage.Events.ChatEvent.OnClientEvent:Connect(function()
	local IB = game:GetService("UserInputService").InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.E then
			print("Triggered.")
            _G.Cancel = true
		end	
	end)
local Thread = coroutine.create(function()
    while task.wait() do
       if _G.Cancel == true then
          IB:Disconnect()
       end
    end
end)
coroutine.resume(Thread)
end)

Sorry for asking, but do you mind helping me with this problem?

Here’s a part of my code that keeps repeating multiple times, how would I extract the remote event functions?

game.ReplicatedStorage.Events.ChatEvent.OnClientEvent:Connect(function()
	game.ReplicatedStorage.Events.ChatValueChangerEvent:FireServer("ChatActivated",true)
	game.ReplicatedStorage.Events.ChatValueChangerEvent:FireServer("ChatSpeaking",true)
end)

(Code is shortened, by the way)

Same thing, just seperate it into a funciton.

local ChatEventCalled = false

local function eventFunction()
if ChatEventCalled == true then return end
ChatEventCalled = true
	game.ReplicatedStorage.Events.ChatValueChangerEvent:FireServer("ChatActivated",true)
	game.ReplicatedStorage.Events.ChatValueChangerEvent:FireServer("ChatSpeaking",true)

end

game.ReplicatedStorage.Events.ChatEvent.OnClientEvent:Connect(function()
eventFunction()
end)
1 Like

Again* You would probably have to do some extra coding to make sure it can be called again like a debounce, I recommend looking into it via youtube, or my person favourite the roblox lua docs

1 Like

Mkay, got it. I’m gonna try it, I’ll probably update about this tomorrow since it’s pretty late for me.

This video is a joke, to be clear.

Had some more issues with the debounce idea on a script I am pretty positive is the best place to fix the problem.

I’ll send it here:

local DialogueIndex = 0
D1 = false
script.Parent.Triggered:Connect(function(player)
	DialogueIndex = 1
	game.ReplicatedStorage.Events.ChatEvent:FireClient(player, "This is gonna be a test", 0.1, Color3.fromRGB(255, 255, 255), Color3.fromRGB(255, 255, 255), workspace.SFX.TypingSounds, true)
	
	game.ReplicatedStorage.Events.MultipleChatEvent.OnServerEvent:Connect(function()
		if DialogueIndex == 1 then
			D1 = true
			DialogueIndex = 2
			game.ReplicatedStorage.Events.ChatEvent:FireClient(player, "...", 0.1, Color3.fromRGB(150, 150, 150), Color3.fromRGB(0, 0, 0), workspace.SFX.TypingSounds, true)
			wait(.1)
			D1 = false
		end	
		if DialogueIndex == 2 and D1 == false then
			D1 = true
			DialogueIndex = 3
			game.ReplicatedStorage.Events.ChatEvent:FireClient(player, "Why am I talking to a Part?", 0.1, Color3.fromRGB(255, 255, 255), Color3.fromRGB(255, 255, 255), workspace.SFX.TypingSounds, true)
			wait(.1)
			D1 = false
		end
		if DialogueIndex == 3 and D1 == false then
			DialogueIndex = 0
			game.ReplicatedStorage.Events.ChatEvent:FireClient(player, "I don't know, you tell me.", 0.1, Color3.fromRGB(150, 150, 150), Color3.fromRGB(0, 0, 0), workspace.SFX.TypingSounds, false)
		end
	end)

	
	
	
end) 

I am almost certain debounce is the way to work around this, since it’s working.
(Note: D1 is the debounce, and yes I have tried doing multiple debounce variables)

It seems I fixed(worked around) the issue (Finally!), also, for anyone wondering, Debounces and values helped me, A LOT.

Yeah, I know kind of. Never nesting isn’t really possible, but I try to avoid nesting as much as possible and pulling things out as I find it makes code so much easier to use and read.

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