Why is my remote function fires multiple times when im only firing it once?

ive tried debounces and cooldowns and none of it is working i dont understand why this is happening maybe because its in a for loop, but idk

--LOCAL SCRIPT
local function getTowerStatus(towerName)
	
	if table.find(playerData.EquippedTowers,towerName) then
		return "Equipped"
		
	elseif table.find(playerData.OwnedTowers,towerName) then
		return "Owned"
		
	else
		return "For Sale"
	end
end

function equipItem(itemName)
	local data = interactItemFunction:InvokeServer(itemName)
	if data then
		playerData = data
		
		equippedTowers()
		updateTowers()
	end
end

function updateTowers()
	tokens.Text = playerData.Tokens
	for i, tower in pairs(towerShop) do
		
		local oldButton = container:FindFirstChild(tower.Name)
		if oldButton then
			oldButton:Destroy()
		end
		
		local newButton = container.Template:Clone()
		
		local status = getTowerStatus(tower.Name)
		
		if status == "Equipped" then
			newButton.Status.Text = "Equipped"
			
		else
			newButton.Status.Text = ""
		end
		
		if status == "Owned" or status == "Equipped" then
			newButton.Name = tower.Name
			newButton.TowerName.Text = tower.Name
			newButton.Visible = true
			newButton.Parent = container
			
			--newButton.MouseButton1Click:Connect(function()
				--equipItem(newButton.Name)
			--end)
			
			newButton.MouseButton1Click:Connect(function()
				
				selectedTower = newButton.Name
				
				local displayModel = Module3D:Attach3D(info.Display,tower.Model:Clone())
				displayModel:SetDepthMultiplier(1.2)
				displayModel.Camera.FieldOfView = 5
				displayModel.Visible = true

				game:GetService("RunService").RenderStepped:Connect(function()
					displayModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
				end)
			end)
				
			info.Equip.MouseButton1Click:Connect(function()
				
				if selectedTower then
					equipItem(selectedTower)
				end
				
			end)
			
			
			local towerModel = Module3D:Attach3D(newButton.Display,tower.Model:Clone())
			towerModel:SetDepthMultiplier(1.2)
			towerModel.Camera.FieldOfView = 5
			towerModel.Visible = true

			game:GetService("RunService").RenderStepped:Connect(function()
				towerModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
			end)
			
		end
	end
end
--SERVER SCRIPT
local function getTowerStatus(player,itemName)
	local playerData = data[player.UserId]
	
	if table.find(playerData.EquippedTowers,itemName) then
		return "Equipped"

	elseif table.find(playerData.OwnedTowers,itemName) then
		return "Owned"

	else
		return "For Sale"
	end
end

replicatedStorage.InteractItem.OnServerInvoke = function(player,itemName)
	local shopItem = towerShop[itemName]
	local playerData = data[player.UserId]
	
	if shopItem and playerData then
		local status = getTowerStatus(player,itemName)
		
		if status == "For Sale" and shopItem.Price <= playerData.Tokens then
			-- purchase
			
			playerData.Tokens -= shopItem.Price
			table.insert(playerData.OwnedTowers,shopItem.Name)
			
			
			elseif status == "Owned" then
				-- equip 
			print("Equipping") --prints 5 times the first time i fire this and then it prints like 2000 times the 2nd time and keeps multiplying everytime i click the equip button
		
				table.insert(playerData.EquippedTowers,shopItem.Name)
				if #playerData.EquippedTowers > maxEquippedTowers then
				table.remove(playerData.EquippedTowers,5)
				
			end

		elseif status == "Equipped" then
			-- unequip
			
			print("Unequipping")  --prints 5 times the first time i fire this and then it prints like 2000 times the 2nd time and keeps multiplying everytime i click the equip button

			if #playerData.EquippedTowers > 1 then
				local towerToRemove = table.find(playerData.EquippedTowers,shopItem.Name)
				table.remove(playerData.EquippedTowers,towerToRemove)
			end
		end
		
		return playerData
	else
		warn("data doesnt exist")
	end
	
	return false
end

replicatedStorage.GetData.OnServerInvoke = function(player)
	return data[player.UserId]
end

https://gyazo.com/c92b7bcca58d0e8fddb9dd59008994c7
gif of whats happening^^^

maybe try adding ‘debounce’ to where it fires the event like this:

local function getTowerStatus(towerName)
	
	if table.find(playerData.EquippedTowers,towerName) then
		return "Equipped"
		
	elseif table.find(playerData.OwnedTowers,towerName) then
		return "Owned"
		
	else
		return "For Sale"
	end
end
local DB = false
function equipItem(itemName)
if DB then
return
end
DB = true
	local data = interactItemFunction:InvokeServer(itemName)
	if data then
		playerData = data
		
		equippedTowers()
		updateTowers()
	end
wait(1)
DB = false
end

function updateTowers()
	tokens.Text = playerData.Tokens
	for i, tower in pairs(towerShop) do
		
		local oldButton = container:FindFirstChild(tower.Name)
		if oldButton then
			oldButton:Destroy()
		end
		
		local newButton = container.Template:Clone()
		
		local status = getTowerStatus(tower.Name)
		
		if status == "Equipped" then
			newButton.Status.Text = "Equipped"
			
		else
			newButton.Status.Text = ""
		end
		
		if status == "Owned" or status == "Equipped" then
			newButton.Name = tower.Name
			newButton.TowerName.Text = tower.Name
			newButton.Visible = true
			newButton.Parent = container
			
			--newButton.MouseButton1Click:Connect(function()
				--equipItem(newButton.Name)
			--end)
			
			newButton.MouseButton1Click:Connect(function()
				
				selectedTower = newButton.Name
				
				local displayModel = Module3D:Attach3D(info.Display,tower.Model:Clone())
				displayModel:SetDepthMultiplier(1.2)
				displayModel.Camera.FieldOfView = 5
				displayModel.Visible = true

				game:GetService("RunService").RenderStepped:Connect(function()
					displayModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
				end)
			end)
				
			info.Equip.MouseButton1Click:Connect(function()
				
				if selectedTower then
					equipItem(selectedTower)
				end
				
			end)
			
			
			local towerModel = Module3D:Attach3D(newButton.Display,tower.Model:Clone())
			towerModel:SetDepthMultiplier(1.2)
			towerModel.Camera.FieldOfView = 5
			towerModel.Visible = true

			game:GetService("RunService").RenderStepped:Connect(function()
				towerModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
			end)
			
		end
	end
end

if it doesnt work try adding ‘debounce’ on the server too.

broooooooooo i swear i did this and it didnt work but whatever sorcery you just did worked

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