Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this happen?

So I've made a custom player list with fancy buttons. Well, apparently somewhere I've made it multiply with each click of different players.. For example:

Output After clicking any activity button:

[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 sent friend request to Player2
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false
[CLIENT] Player1 false

What did I do wrong?

local list = script.Parent.list
local bg = script.Parent

local player = game.Players.LocalPlayer

local addFriend, removeFriend = game.ReplicatedStorage.CrossConnections.Friends.AddFriend, game.ReplicatedStorage.CrossConnections.Friends.RemoveFriend
local isFriend = game.ReplicatedStorage.CrossConnections.Friends.GetIsFriend

local playerSlot = script.PlayerSlot

local function update()
	list:ClearAllChildren()
	local numPlayers = #game.Players:GetChildren()
	
	bg.Size = UDim2.new(0.2,0,0.1,(numPlayers*30)-30)
	
	for i, v in pairs(game.Players:GetChildren()) do
		local newSlot = playerSlot:Clone()
		newSlot.Text = v.Name
		newSlot.Position = UDim2.new(0,0,0,30*(i-1))
		newSlot.Parent = list
		
		local slotDebounce = 0
		
		newSlot.MouseButton1Click:connect(function()
			if slotDebounce == 0 then
				slotDebounce = 1
				for i, v in pairs(list:GetChildren()) do
					v:FindFirstChild("ActivityList").Visible = false
				end
				
				if newSlot.Text ~= player.Name then
					newSlot:FindFirstChild("ActivityList").Visible = true
				end
				
				local activityList = newSlot:FindFirstChild("ActivityList")
				local toggle, tele, repo = activityList.ToggleFriend, activityList.Teleport, activityList.Report
				
				local friend = isFriend:InvokeServer(newSlot.Text)
				_G.output(player.Name, friend)
				
				if friend then
					toggle.Text = "Remove Friend"
				else
					toggle.Text = "Add Friend"
				end
				
				local activityDebounce = 0
				
				toggle.MouseButton1Click:connect(function()
					if activityDebounce == 0 then
						activityDebounce = 1
						if toggle.Text == "Add Friend" then
							_G.output(player.Name, "sent friend request to", newSlot.Text)
							addFriend:InvokeServer(newSlot.Text)
						else
							_G.output(player.Name, "is no longer friends with", newSlot.Text)
							removeFriend:InvokeServer(newSlot.Text)
						end
						
						local friend = isFriend:InvokeServer(newSlot.Text)
						_G.output(player.Name, friend)
						
						if friend then
							toggle.Text = "Remove Friend"
						else
							toggle.Text = "Add Friend"
						end
						activityDebounce = 0
					end
				end)
				
				tele.MouseButton1Click:connect(function()
					if activityDebounce == 0 then
						activityDebounce = 1
						--do fancy stuffs
						activityDebounce = 0
					end
				end)
				
				repo.MouseButton1Click:connect(function()
					if activityDebounce == 0 then
						activityDebounce = 1
						--do fancy stuffs
						activityDebounce = 0
					end
				end)
				slotDebounce = 0
			end
		end)
	end
end

update()

local lastCheckedNumPlayers

while wait(1) do
	if lastCheckedNumPlayers ~= #game.Players:GetChildren() then
		update()
		lastCheckedNumPlayers = #game.Players:GetChildren()
	end
end

3 comments

0

Could you give us a more descriptive example so we know what we're looking for whilst trawling through your Great Wall of Code?

0

Correction: It only does mass dupes in the output when I click an activity button.

0

A little bit of description can go a long way.

1 answer

✓ Accepted answer

Every time you click the button assigned to newSlot, it binds a new event to toggle. toggle is not a new object every time that it is bound, so the function is bound multiple times, which stacks every time you click newSlot.

The way to get around this is to either bind it ahead of time, or to keep track of your connections and disconnect them before making new ones.

This discussion is preserved as it was. New answers, comments, and votes are closed.