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
Tried doing debounces but it only broke more stuff. Any ideas?
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
It’s a little vague but I can think of 2 reasons why this could’ve happened.
Multiple server-side receivers. This means that there could be multiple OnServerEvent connections, and a new one is made every time you die.
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?
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
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?
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)