Do you have a corresponding Connect event to detect when MouseButton1Down has ended and to Disconnect Mouse.Move:Connect & Input.InputBegan:Connect & Mouse.Button1Down:Connect. If not, then they will run continuously.
When you click on the button, three events are connected together. One for moving the part when the mouse moves, one for rotating the part when the player presses R, and one for firing the positional data of the part.
The problem is, they don’t disconnect, which is why they continually go on and on forever.
Instead, create two variables for the two events before this event: Mouse.Button1Down. Then, in that event, run Variable1:Disconnect() and Variable2:Disconnect()
Also, instead of using Mouse.Button1Down:Connect use Mouse.Button1Down:Once
local Buttons = script.Parent.Frame
Buttons.Part.Button.MouseButton1Down:Connect(function()
script.Parent.Enabled = false
local Part = Buttons.Part.ViewportFrame.Part:Clone()
Part.Parent = workspace
Part.Anchored = true
Part.CanCollide = false
Part.Size /= Vector3.new(5,5,5)
local Mouse = game.Players.LocalPlayer:GetMouse()
local Input = game:GetService('UserInputService')
Mouse.TargetFilter = Part
local Variable1, Variable2
Variable1 = Mouse.Move:Connect(function()
Part.Position = Mouse.Hit.Position
Part.Position = Vector3.new(math.round(Part.Position.X), math.round(Part.Position.Y), math.round(Part.Position.Z))
Part.Position += Vector3.new(0,Part.Size.Y/2,0)
end)
Variable2 = Input.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.R then
Part.Orientation += Vector3.new(0,45,0)
end
end)
Mouse.Button1Down:Once(function()
script.Parent.Enabled = true
Variable1:Disconnect()
Variable2:Disconnect()
script.Fire:FireServer(Part.CFrame, Part.Size)
Part:Destroy()
return
end)
end)
When you have finished the placement and released MouseButton1, the Button1Up()`` event will fire. It is at this point that 3 the Connects you have set in the MouseButton1Down ``` function need to be Disconnected, otherwise they will continue to fire.
I would:
Declare new variables to hold the Connect events at the top of the script (but not set them):
local MouseMoveEvent
local InputBeganEvent
local MouseButton1DownEvent
In the main Buttons.Part.Button.MouseButton1Down:Connect event, set these variables as the Connect(function) stuff, ie:
MouseMoveEvent = Mouse.Move:Connect(function()
Having them declared as variables allows us to Disconnect them in the future in a Buttons.Part.Button.MouseButton1Up event. SO for step2, we would have:
Does that make sense? It’s good to Disconnect event once we have used the a) stop them firing repeatedly and b) free up that memory if it is no longer required.