RemoteEvent fires multiple times after player died

As in the title, I dont know what the cause of this. In my main Server script, im reciving RemoteEvent being fired from client. Every time character resets or dies it “adds” another RemoteEvent fired signal. I don’t want this to happen.

RemoteEvent being fired - LocalScript in PlayerGUI

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local StatsGUI = script.Parent

local Plr = game:GetService("Players").LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
print(Char.Name)

for i, v in StatsGUI:GetChildren() do
	if v:IsA('TextLabel') and Char then
		local vChild = v:GetChildren()
		for btn, _ in vChild do
			if vChild[btn] ~= nil and vChild[btn]:IsA('TextButton') then
				local vButton = vChild[btn]
				vButton:SetAttribute('Upgrade', v.Name)
				
				local function isClicked()
					local ATTRIBS = Char.DATA.ATTRIBS
					
					--v.Text = v.Name..': '..ATTRIBS:GetAttribute(vButton:GetAttribute('Upgrade'))
					--print('ADDED +1 TO: '..vButton:GetAttribute('Upgrade'))
					print('SENT BTN INFO')
					ReplicatedStorage.Client.Events.Update_STATS:FireServer(vButton:GetAttribute('Upgrade'))
				end
				
				vButton.Activated:Connect(isClicked)
			end
		end
	end
end

Server-side reciver

		ReplicatedStorage.Client.Events.Update_STATS.OnServerEvent:Connect(function(Player, Value)
			if Player.Name == Plr.Name and Player.Character then
				PlayerData[Plr.UserId]['ATTRIBS'][Value] += 1
				print('RECIVED')
			end
		end)

Console Log

image

Tried doing debounces but it only broke more stuff. Any ideas?

1 Like

Try to disable “Reset On Spawn” property of screen gui
image

2 Likes

The same thing happens. Nothing’s changed.

hmmm, so, try this code:

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local StatsGUI = script.Parent

local Plr = game:GetService("Players").LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
print(Char.Name)

local connection, first = nil,true

for i, v in StatsGUI:GetChildren() do
	if v:IsA('TextLabel') and Char then
		local vChild = v:GetChildren()
		for btn, _ in vChild do
			if vChild[btn] ~= nil and vChild[btn]:IsA('TextButton') then
				local vButton = vChild[btn]
				vButton:SetAttribute('Upgrade', v.Name)

				local function isClicked()
					local ATTRIBS = Char.DATA.ATTRIBS

					--v.Text = v.Name..': '..ATTRIBS:GetAttribute(vButton:GetAttribute('Upgrade'))
					--print('ADDED +1 TO: '..vButton:GetAttribute('Upgrade'))
					print('SENT BTN INFO')
					ReplicatedStorage.Client.Events.Update_STATS:FireServer(vButton:GetAttribute('Upgrade'))
				end
				
				if not first then
					if connection then
						connection:Disconnect()
					end
				else
					first = false
					connection = vButton.Activated:Connect(isClicked)
				end
			end
		end
	end
end
1 Like

It’s a little vague but I can think of 2 reasons why this could’ve happened.

  1. Multiple server-side receivers. This means that there could be multiple OnServerEvent connections, and a new one is made every time you die.
  2. LocalScript doesn’t get removed when you die, resulting in multiple of them that listen to when you click a button, which results in the remote being fired multiple times.

Though about your solution, however, now it doesn’t seem to be detecting Activated event. Clicking buttons does nothing nor it prints anything somehow.

2nd option might be the case, because my LocalScript is located inside PlayerGUI. When Character dies, script is not being removed and another connection is added, because new Character got added?

I think you have to try this updated code:

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local StatsGUI = script.Parent

local Plr = game:GetService("Players").LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
print(Char.Name)

local connections = {}

for _, connection in ipairs(connections) do
	connection:Disconnect()
end
table.clear(connections)

for i, v in StatsGUI:GetChildren() do
	if v:IsA('TextLabel') then
		local vChild = v:GetChildren()
		for btn, _ in vChild do
			if vChild[btn] and vChild[btn]:IsA('TextButton') then
				local vButton = vChild[btn]
				vButton:SetAttribute('Upgrade', v.Name)

				local function isClicked()
					local ATTRIBS = Char:WaitForChild("DATA"):WaitForChild("ATTRIBS")
					print('SENT BTN INFO')
					ReplicatedStorage.Client.Events.Update_STATS:FireServer(vButton:GetAttribute('Upgrade'))
				end

				local connection = vButton.Activated:Connect(isClicked)
				table.insert(connections, connection)
			end
		end
	end
end

Its not working again.
@thom463s might be onto something. What if I replicate my GUIs and just give them to players after their character has loaded?

Its strange that your script works several times at all, which creates new connections. Have you tried putting the script in another folder?

You mean somewhere that is not GUI?

Yea, try to place it in StarterPlayerScripts.

Now after I reset I get no connections at all. I might try my previous code.

EDIT: Previous code does the same

Ima try StarterCharacterScripts. That way my script is loaded every time character exists

EDIT: Still does the same thing

Can you upload a copy of the place so that I can go deeper into the problem?

I might know whats the cause of this. My OnFireEvents are declared inside Plr.CharacterAdded. Does that mean Im creating new connection everytime Character gets added?

I think yes

characters limitcharacters limitcharacters limitcharacters limitcharacters limitcharacters limit

1 Like

As I though, Plr.CharacterAdded was creating another connection EVERYTIME Character is added. Just moved this bit of code into Plrs.PlayerAdded so it loads only once, when Player joins the game.

ReplicatedStorage.Client.Events.Update_STATS.OnServerEvent:Connect(function(Player, Value)
    if Player.Name == Plr.Name and Player.Character then
		PlayerData[Plr.UserId]['ATTRIBS'][Value] += 1
		print('RECIVED')
	end
end)

Thank you guys for helping!

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