Script never ends

I am making a placing system and it seems this script never ends like if I rotate it everyway then it will start to make circle type things.

Ignore the viewportframe stuff

LocalScript
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
	
	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)
	
	Input.InputBegan:Connect(function(Input)
		if Input.KeyCode == Enum.KeyCode.R then
			Part.Orientation += Vector3.new(0,45,0)
		end
	end)
	
	Mouse.Button1Down:Connect(function()
		script.Parent.Enabled = true
		
		script.Fire:FireServer(Part.CFrame, Part.Size)
		Part:Destroy()
		return
	end)
end)

The script you showed me doesn’t contain any loops that could repeat indefinitely. Maybe the problem might be the Part.Orientation line.

I recommend using Part:PivotTo() to change the rotation of the part.

Part:PivotTo(Part:GetPivot() * CFrame.Angles(0,math.rad(45),0)) -- Rotates the part 45 degrees clockwise.

You should probably understand about CFrames and PVInstance.

ok I’ll try this rq

[CharLimit]

nah it still does it
image

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.

a what what

[CharLimitRemover]

oh I see no I don’t you can see my entire script

I gtg in like 5 min

[CharLimit]

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)
1 Like

so I just replace mine with this?

Yes, you can.

Character Limit

Works thank you so much (the people that use roblox really know how to script fast)

1 Like

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:

  1. Declare new variables to hold the Connect events at the top of the script (but not set them):
local MouseMoveEvent
local InputBeganEvent
local MouseButton1DownEvent
  1. 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:

Buttons.Part.Button.MouseButton1Up:Connect(function()
	MouseMoveEvent:Disconnect()
end)

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.

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