Remote event exponentially getting fired

sorry if this is unclear or isnt written correctly, as this is my first post.

ive been trying to make a system to where if you click, the client detects it, and sends it over to the server using a remoteevent, to update a “punches” variable

the issue is, when i do this, the removeevent gets fired more and more times every time i click
it also starts out firing like 80 times

1 click - 80 fires
2 clicks - 160 fires

you get the idea

i need it to be for every click, a remoteevent gets fired once.

ive tried everything that comes to my mind as a new roblox dev, like debounces, but nothing seems to work

clientside:

local uis = game:GetService("UserInputService")
local debounce = true
local run = Instance.new("IntValue")
run.Value = 0
uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("mouse down")
		wait()
		if debounce == true then
			wait()
			game.ReplicatedStorage.Punch:FireServer()
			run.Value += 1
			print(run.Value)
			debounce = false
			print(debounce)
			uis.InputEnded:Connect(function(input)
				print(input.UserInputType)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					print("mouse up")
					wait(0.2)
					debounce = true
				end
			end)
		end
	end
end)

server script:

local enterEvent = game.ReplicatedStorage.Going_In
local punchEvent = game.ReplicatedStorage.Punch


enterEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	local activated = Instance.new("IntValue")
	activated.Value = 0
	while humanoid.Health > 0 do
		wait()
		punchEvent.OnServerEvent:Connect(function(player,debounce)
			print("recieved")
			activated.Value += 1
			print(activated.Value)
			print(debounce)
			local leaderstats = player:WaitForChild("leaderstats")
			local punches = leaderstats:WaitForChild("Punches")
			punches.Value += 1
		end)
	end
end)

1 Like

also i forgot to mention, but the “run” intvalue was meant for me trying to debug whatever was happening, same with the “activated” int

1 Like

First, place this outside the function connected to the uis.InputBegan event, then test and tell me if there is a difference

Either that or do

connection = uis.InputEnded:Connect(input)
 if input.UserInputType == Enum.UserInputType.MouseButton1 then
  connection:Disconnect()
 end
end)

if i held mouse1 for too long, it still sent 100 different serverfires, and also neither worked.

however, after that initial click, nothing went through

Nevermind, this part of the code is the issue. You are connecting the punchEvent every wait(). Remove the loop and try not using two remote events when you can use one.

1 Like
punchEvent.OnServerEvent:Connect(function(player,debounce)
            local character = player.Character
	        local humanoid = character:FindFirstChild("Humanoid")
			local leaderstats = player:WaitForChild("leaderstats")
			local punches = leaderstats:WaitForChild("Punches")
			punches.Value += 1
end)

should look something like this

1 Like

i created enterEvent to send a signal every time your character teleports into the map through a portal, and i detect every time the player punches with punchEvent.

is there another way to know when the player entered the map through the portal, and only fire punch once you used the portal?

can i just make enterEvent fire to the client and check for punches there?

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

local enterEvent = ReplicatedStorage.Going_In
local punchEvent = ReplicatedStorage.Punch

local playersEntered = {}

-- Handling portal
enterEvent.OnServerEvent:Connect(function(player)
  local character = player.Character
  local humanoid = character:FindFirstChild("Humanoid")
  table.insert(playersEntered, player)
end)

-- Handling punching
punchEvent.OnServerEvent:Connect(function(player,debounce)
  if table.find(playersEntered, player) then
    local leaderstats = player:WaitForChild("leaderstats") 
    local punches = leaderstats:WaitForChild("Punches")
    punches.Value += 1
  end
end)

-- If a player leaves then remove them from the table
Players.PlayerRemoving:Connect(function()
  if table.find(playersEntered, player) then
    table.remove(playersEntered, table.find(playersEntered, player))
  end
end)

You can connect them separetely and use a table to know who is in and who isnt. Use table.clear(playersEntered) if you bring all players back or something

2 Likes

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