Rotational Script bug

this is the issue:

Rotational Part of LocalScript
mouse.Button2Down:Connect(function() -- change angle when right click
	angle = angle + math.rad(45)
	FakeBuilding:SetPrimaryPartCFrame(BuildingCF * CFrame.Angles(0,angle,0))
end)
Entire LocalScript
local BuildingCF = CFrame.new()
local PlaceObject = game.ReplicatedStorage.Events.PlaceBuilding
local mouse = game.Players.LocalPlayer:GetMouse()

local uis = game:GetService("UserInputService")

local FakeBuilding = game.ReplicatedStorage.Spawnables.Buildings.FakeBuilding

local plr = game.Players.LocalPlayer
local Team = plr.Team

local isColliding = false

local angle = 0

mouse.TargetFilter = FakeBuilding

local Snap = 1

local shouldBeFiring = true


local function GetTouchingParts(part)
   local connection = FakeBuilding.PrimaryPart.Touched:Connect(function() end)
   local results = FakeBuilding.PrimaryPart:GetTouchingParts()
   connection:Disconnect()
   return results
end


for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("ImageButton") then
	
	local button = v
	
	v.MouseButton1Click:Connect(function()
		shouldBeFiring = true
		
		FakeBuilding.PrimaryPart.Size = game.ReplicatedStorage.Spawnables.Buildings[v.Name].PrimaryPart.Size		
		
		script.Parent.Parent.Adornee = nil
		script.Parent.Visible = false
		
		FakeBuilding.Parent = workspace
		
		if shouldBeFiring == true then
			mouse.Move:Connect(function()
								
				local results = GetTouchingParts()
				
				if #results <= 0 then -- no collison
					FakeBuilding.PrimaryPart.SelectionBox.Color3 = Color3.fromRGB(0, 255, 127) -- green
					FakeBuilding.PrimaryPart.SelectionBox.SurfaceColor3 = Color3.fromRGB(0, 255, 127) -- green
					isColliding = false
				else -- with collison
					FakeBuilding.PrimaryPart.SelectionBox.Color3 = Color3.fromRGB(234, 58, 14) -- red
					FakeBuilding.PrimaryPart.SelectionBox.SurfaceColor3 = Color3.fromRGB(234, 58, 14) -- red
					isColliding = true
				end
				
				BuildingCF = CFrame.new(
					math.floor(mouse.Hit.X / Snap + 0.5) * Snap,
					1,
					math.floor(mouse.Hit.Z / Snap + 0.5) * Snap
				)
					
				FakeBuilding:SetPrimaryPartCFrame(BuildingCF * CFrame.Angles(0,angle,0))
				
				while shouldBeFiring == true do	
					wait()
					workspace:WaitForChild("CamBlock").Position = workspace.Enviroment.Teams[Team.Name].Base_Part.Home.Hitbox.Position + Vector3.new(0,30,0)
				end
		
				
				
				
			end)
		end
		
		
		mouse.Button2Down:Connect(function() -- change angle when right click
			angle = angle + math.rad(45)
			FakeBuilding:SetPrimaryPartCFrame(BuildingCF * CFrame.Angles(0,angle,0))
		end)
		
		
		mouse.Button1Down:Connect(function()
			if shouldBeFiring == true then
				if isColliding == false then
					PlaceObject:FireServer(BuildingCF, button, angle)
					shouldBeFiring = false
				end
			end
			if shouldBeFiring == false then
				FakeBuilding.Parent = game.ReplicatedStorage
			end
		end)
	end)
	end	
end
Script
local PlaceObject = game.ReplicatedStorage.Events.PlaceBuilding

PlaceObject.OnServerEvent:Connect(function(player, BuildingCF, button, angle)
	local building = game.ReplicatedStorage.Spawnables.Buildings[button.Name]:Clone()
	building:SetPrimaryPartCFrame(BuildingCF * CFrame.Angles(0,angle,0))
	building.Parent = workspace
		
	for i,v in pairs(building:GetChildren()) do
		if v.Name == "Main" then
			v.BrickColor = player.TeamColor
		end
	end	
end)

You need to disconnect your mouse events when you are finished placing down the building:

local connection = mouse.Button2Down:Connect(function()
    --your code
end)

mouse.Button1Down:Connect(function()
    if shouldBeFiring == true then
        if isColliding == false then
            PlaceObject:FireServer(...)

            -- add this especially
            connection:Disconnect()

            shouldBeFiring = false
        end
    elseif -- the rest of your code
end)

You might want to do this with the rest of your event connections to avoid memory leaks, but save that for optimizing later on.

1 Like

Thanks, ill be sure to set this up with my other events.

1 Like