Why server sends a lot of Invoke?

			UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.C then
					local StructureCFrame = clientStructure.CFrame
					placingStructure = false
					clientStructure:Destroy()
					StructureFrame.Visible = true
					game.ReplicatedStorage.MoveStructure:InvokeServer(clientStructure.Name, startpos)
					for i,v in pairs(workspace:FindFirstChild(player.Name.."City"):GetChildren()) do
						if v.Name == "PartPlace" then
							v.Texture.Transparency = 1
						end
					end
				end
			end)

As soon as I press C, the server script fires many times
(Part of local script)

It is possible that you’ve connected the InputBegan > KeyCode == C function more than once, perhaps inside a loop. You should check if that’s the case, or send your code for us to analyse.

You should put in a debounce:

local debounce = true
UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.C and debounce then
                    debounce = false
					local StructureCFrame = clientStructure.CFrame
					placingStructure = false
					clientStructure:Destroy()
					StructureFrame.Visible = true
					game.ReplicatedStorage.MoveStructure:InvokeServer(clientStructure.Name, startpos)
					for i,v in pairs(workspace:FindFirstChild(player.Name.."City"):GetChildren()) do
						if v.Name == "PartPlace" then
							v.Texture.Transparency = 1
						end
					end
                    debounce = true
				end
			end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MoveStructure = ReplicatedStorage:WaitForChild("MoveStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local StructureFrame = player.PlayerGui.MainGui.StructureFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local mouse = player:GetMouse()

local yBuildingOffset = 0
local maxPlacingDistance = 50
local rKeyIsPressed = false
local placingStructure = false
local MovePart
local db = true
game.ReplicatedStorage.Events.Select.OnClientEvent:Connect(function(cube)
	MovePart =  cube
	db = true
end)
script.Parent.MouseButton1Click:Connect(function()
	print(MovePart)
	for i,v in pairs(workspace:FindFirstChild(player.Name.."City"):GetChildren()) do
		if v.Name == "PartPlace" then
			v.Texture.Transparency = 0
		end
	end
	StructureFrame.Visible = false

	local yOrientation = 0
	local goodToPlace = false
	local placedStructure
	local startpos = MovePart.CFrame
	if placingStructure == false then
		placingStructure = true

		local clientStructure = Structures:FindFirstChild(MovePart.Parent.Name):Clone()
		MovePart.Parent:Destroy()
		if clientStructure.ClassName == "UnionOperation" then
			clientStructure.UsePartColor = true
		end
		clientStructure.BrickColor = BrickColor.new("Forest green")
		clientStructure.Material = "Neon"
		clientStructure.Cube.BrickColor = BrickColor.new("Forest green")
		clientStructure.Cube.Material = "Neon"
		clientStructure.CanCollide = false
		clientStructure.Parent = game.Workspace

		local startingCFrame = CFrame.new(0,0,0)
		clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
		print(startpos)
		RunService.RenderStepped:Connect(function()
			local mouseRay = mouse.UnitRay
			local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
			local ignoreList = {clientStructure, char}
			local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
			if hit and hit.Name == "PartPlace" and hit.Parent.Name == player.Name.."City" and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDistance then
				goodToPlace = true
				clientStructure.BrickColor = BrickColor.new("Forest green")
				if clientStructure:FindFirstChild("Cube") then
					clientStructure.Cube.BrickColor = BrickColor.new("Forest green")
					clientStructure.Cube.SelectionBox.Color3 = Color3.fromRGB(0,255,0)
				end
			else
				goodToPlace = false
				clientStructure.BrickColor = BrickColor.new("Crimson")
				if clientStructure:FindFirstChild("Cube") then
					clientStructure.Cube.BrickColor = BrickColor.new("Crimson")
					clientStructure.Cube.SelectionBox.Color3 = Color3.fromRGB(255,0,0)
				end
			end
			local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
			local newCFrame = CFrame.new(math.floor((position.X -workspace:FindFirstChild(player.Name.."City").PartPlace.Position.X)/2)*2+workspace:FindFirstChild(player.Name.."City").PartPlace.Position.X+1.9/2,position.Y + clientStructure.Size.Y/2,math.floor((position.Z - workspace:FindFirstChild(player.Name.."City").PartPlace.Position.Z)/2)*2+workspace:FindFirstChild(player.Name.."City").PartPlace.Position.Z+2.35/2)
			clientStructure.CFrame = newCFrame * newAnglesCFrame
			if clientStructure:FindFirstChild("Cube") then
				clientStructure.Cube.CFrame = newCFrame * newAnglesCFrame
			end
			UIS.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					if placingStructure == true then
						if goodToPlace == true then
							local StructureCFrame = clientStructure.CFrame
							placingStructure = false
							clientStructure:Destroy()
							StructureFrame.Visible = true
							game.ReplicatedStorage.MoveStructure:InvokeServer(clientStructure.Name, StructureCFrame)
							for i,v in pairs(workspace:FindFirstChild(player.Name.."City"):GetChildren()) do
								if v.Name == "PartPlace" then
									v.Texture.Transparency = 1
								end
							end
						end
					end
				end
				local debounce = true
				UIS.InputBegan:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.C and debounce then
						debounce = false
						local StructureCFrame = clientStructure.CFrame
						placingStructure = false
						clientStructure:Destroy()
						StructureFrame.Visible = true
						game.ReplicatedStorage.MoveStructure:InvokeServer(clientStructure.Name, startpos)
						for i,v in pairs(workspace:FindFirstChild(player.Name.."City"):GetChildren()) do
							if v.Name == "PartPlace" then
								v.Texture.Transparency = 1
							end
						end
						debounce = true
					end
				end)
				if input.KeyCode == Enum.KeyCode.R then
					rKeyIsPressed = true
					local rotationSpeed = 45
					yOrientation = yOrientation + rotationSpeed		
				end
			end)
		end)
	end
end)

Here is all script

[/quote]

The InputBegan functions are connecting more than once. More specifically, every time the InputBegan event fires, the function connected to it will run as many times as the RenderStepped and script.Parent.MouseButton1Click events were fired. Therefore, you should remove the InputBegan function from being an inner function, and make it independent.

1 Like