Remote Function Running multiple times

When I invoke the remote function in the module script, it spawns multiple units

Local Script:

UserInputService.InputBegan:Connect(function(input, processed)
			local AllowedToSpawn = requestTowerFunction:InvokeServer(Unit.Name)
			if processed then
				return
			end

			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				

				if CanPlace and HasPlaceHolder and AllowedToSpawn then
					

					print("Click")

					local PlacedTower = SpawnUnitFunction:InvokeServer(UnitToSpawn.Name, UnitToSpawn.PrimaryPart.CFrame)
					if PlacedTower then
						RemovePlaceHolderUnit()	



						TweenService:Create(button, info, {Size = UDim2.new(0.7,0,0.7,0)}):Play()
						TweenService:Create(Shadow and ShadowOutline, info, {Transparency = 1}):Play()
						ToggleTowerInfo()

						HasPlaceHolder = false
						
					end
					
				else
					RemovePlaceHolderUnit()
					HasPlaceHolder = false
					TweenService:Create(button, info, {Size = UDim2.new(0.7,0,0.7,0)}):Play()
					TweenService:Create(Shadow and ShadowOutline, info, {Transparency = 1}):Play()



				end
			end

Module script:

function UnitSpawnModule.Spawn(player, name, cframe, previous)
	local AllowedToSpawn = UnitSpawnModule.CheckSpawn(player, name)
	local Count = 0
	
	
	--for i,v in pairs(workspace.Units:GetChildren()) do
		--if v.Name == name then
			--Count+=1
		--end
	--end
	--if ReplicatedStorage.Units[name].Config.Limit then
		--if Count >= ReplicatedStorage.Units[name].Config.Limit.Value then
			--AllowedToSpawn = false -- Limit reached
		--end
	--else
		--warn(name.."has no spawn limit")
	--end

	
	
	if AllowedToSpawn and Debounce == false then
		if Debounce then return end
		Debounce = true
		local newUnit
		
		if previous then
			previous:Destroy()
			newUnit = ReplicatedStorage.Units.Upgrades[name]:Clone()
		else
		    newUnit = ReplicatedStorage.Units[name]:Clone()
		end
		
		
		newUnit:WaitForChild("HumanoidRootPart").CFrame = cframe
		newUnit.Parent = workspace.Units
		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newUnit.HumanoidRootPart.CFrame
		bodyGyro.Parent = newUnit.HumanoidRootPart

		
		
		local UnitNumberExists = player:FindFirstChild(name)

		if UnitNumberExists then
			local UnitNumber = player:FindFirstChild(name)
			UnitNumber.Value += 1
			
		else
			local UnitNumber = Instance.new("IntValue")
			UnitNumber.Name = name
			UnitNumber.Parent = player
			UnitNumber.Value = 0
			UnitNumber.Value +=1
			UnitAddEvent:FireClient(player)
		end
		
		
			newUnit.HumanoidRootPart:SetNetworkOwner(nil)
			--local humPart = newUnit:WaitForChild("HumanoidRootPart")


			
			player.Money.Value -= newUnit.Config.Cost.Value
			
		local cloneSound = gameSFXFolder.Coins.CoinPlace:Clone()
		local attachment = Instance.new("Attachment")
		attachment.Name = "PlaceSound"
		attachment.Parent = newUnit.HumanoidRootPart
		cloneSound.Parent = attachment
		cloneSound:Play()

		MoneyGoneEvent:FireClient(player, newUnit.Config.Cost.Value)
		Debounce = false


			coroutine.wrap(UnitSpawnModule.Attack)(newUnit, player)
			
			return newUnit 
			

	
		
	else
		warn("Requested Unit does not exist: "..name)
		return false
	end
end

SpawnUnitFunction.OnServerInvoke = UnitSpawnModule.Spawn

What seems to be the problem

You should add a debounce for the Spawn function.
Edit: I noticed that you already had it, you could add a slight delay to prevent spawning multiple of units.

2 Likes

Thanks mann I got it working nowww!!!

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